我是ggplot的新手所以请耐心等待.我出来的图表增长预测为35小面积的地区这是一个阴谋即使使用美妙的不健康的量directlabels
库.但是我需要所有系列进行初步筛选.
挑战是使其可读.我找到了@Ben Bolker使用大量不同颜色的修复,但是在改变线型方面遇到了麻烦.35系列不需要是唯一的,但我想使用12种不同的类型来使单个系列更容易阅读.
我的计划是创建一个包含12种可能类型的35个元素的随机列表,并将其作为linetype参数传递,但我无法使其工作,但错误:
Error: Aesthetics must either be length one, or the same length as the dataProblems:lty
Run Code Online (Sandbox Code Playgroud)
我在linetype列表中有35个值.当然,我希望类型,颜色和所有内容都能反映在图例中.
融化的数据看起来像这样; 对35个系列中的每一个进行了9年的观察:
> 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
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的代码:
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)
Run Code Online (Sandbox Code Playgroud)
加入
scale_linetype_manual("",values=lty$lty) +
Run Code Online (Sandbox Code Playgroud)
在scale_color_manual而不是linetype参数之后生成图表,但行都是相同的.那么,如何为大型系列计数改变线条?
mne*_*nel 10
使用的技巧scale_..._manual
通常是发送一个命名向量作为value
参数.这个setNames
功能很好用
首先,一些虚拟数据
## 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))
Run Code Online (Sandbox Code Playgroud)
接下来,我们创建一个矢量,它是1:12的随机样本(替换)并将名称设置为与geography
变量相同
lty <- setNames(sample(1:12,35,T), levels(simulation_long_index$geography))
Run Code Online (Sandbox Code Playgroud)
这就是它的样子
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
Run Code Online (Sandbox Code Playgroud)
现在你可以line_type = geography
结合使用了scale_linetype_manual(values = lty)
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)
Run Code Online (Sandbox Code Playgroud)
哪个给你
顺便说一句,你真的想把这些年作为因子变量吗?
归档时间: |
|
查看次数: |
5429 次 |
最近记录: |