
Untitled
By: a guest on
Aug 8th, 2012 | syntax:
None | size: 2.31 KB | hits: 14 | expires: Never
Passing variable with line types to ggplot linetype
Error: Aesthetics must either be length one, or the same length as the dataProblems:lty
> simulation_long_index[16:24,]
year geography value
16 2018 sfr_2 101.1871
17 2019 sfr_2 101.1678
18 2020 sfr_2 101.2044
19 2012 sfr_3 100.0000
20 2013 sfr_3 100.1038
21 2014 sfr_3 100.2561
22 2015 sfr_3 100.0631
23 2016 sfr_3 100.8071
24 2017 sfr_3 101.2405
lty <- data.frame(lty=letters[1:12][sample(1:12, 35,replace=T)])
g3<-ggplot(data=simulation_long_index,
aes(
x=as.factor(year),
y=value,
colour=geography,
group=geography,
linetype=lty$lty))+
geom_line(size=.65) +
scale_colour_manual(values=manyColors(35)) +
geom_point(size=2.5) +
opts(title="growth")+
xlab("Year") +
ylab(paste("Indexed Value (Rel. to 2012")) +
opts(axis.text.x=theme_text(angle=90, hjust=0))
print(g3)
scale_linetype_manual("",values=lty$lty) +
## some dummy data
simulations<- expand.grid(year = 2012:2020, geography = paste0('a',1:35))
library(plyr)
library(RColorBrewer)
simulation_long_index <- ddply(simulations, .(geography), mutate,
value = (year-2012) * runif(1,-2, 2) + rnorm(9, mean = 0, sd = runif(1, 1, 3)))
## create a manyColors function
manyColors <- colorRampPalette(brewer.pal(name = 'Set3',n=11))
lty <- setNames(sample(1:12,35,T), levels(simulation_long_index$geography))
lty
## a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16
## 7 5 8 11 2 10 3 2 5 4 6 6 11 8 2 2
## a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 a32
## 12 7 6 8 11 5 1 1 8 12 8 1 12 2 3 5
## a33 a34 a35
#7 1 3
ggplot(data=simulation_long_index,
aes(
x=as.factor(year),
y=value,
colour=geography,
group=geography,
linetype = geography))+
geom_line(size=.65) +
scale_colour_manual(values=manyColors(35)) +
geom_point(size=2.5) +
opts(title="growth")+
xlab("Year") +
ylab(paste("Indexed Value (Rel. to 2012")) +
opts(axis.text.x=theme_text(angle=90, hjust=0)) +
scale_linetype_manual(values = lty)