将带有线型的变量传递给 ggplot 线型 [英] Passing variable with line types to ggplot linetype
问题描述
我是 ggplot 的新手,所以请耐心等待.我正在绘制 35 个小区域地理区域的增长预测,即使使用了出色的 directlabels
库,这对于一个地块来说也是一个不健康的数量.但是我需要所有系列进行初步筛选.
I am new to ggplot so bear with me. I am charting out growth projections for 35 small-area geographies which is an unhealthy amount for one plot even with use of the fantastic directlabels
library. However I need all the series for initial screening.
挑战在于使其具有可读性.我找到了@Ben Bolker 的修复方法,用于使用 大量不同的颜色,但我无法改变线型.35 系列不需要是唯一的,但我想使用 12 种不同的类型,使单个系列更易于阅读.
The challenge is to make it readable. I found a fix by @Ben Bolker for using large numbers of distinct colors but am having trouble varying the linetype. The 35 series don't need to be unique, but I would like to use the 12 different types to make individual series easier to read.
我的计划是创建一个随机列表,其中包含 12 种可能类型的 35 个元素,并将其作为 linetype 参数传递,但我无法使其正常工作,并出现错误:
My plan was to create a random list with 35 elements of the 12 possible types and pass that as the linetype argument, but I am having trouble getting it to work, with the error:
Error: Aesthetics must either be length one, or the same length as the dataProblems:lty
我的线型列表中有 35 个值.当然,我希望类型、颜色和所有内容都反映在图例中.
I have 35 values in the linetype list. Of course I would like for the types, colors and all to be reflected in the legend.
融化的数据是这样的;35 个系列中每个系列的 9 年观察:
The melted data looks like this; 9 years' observations for each of 35 series:
> 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) +
在 scale_color_manual 而不是 linetype 参数后生成图表,但线条都是相同的.那么,对于大系列计数,我如何使线条发生变化?
after scale_color_manual instead of the linetype argument produces the chart, but lines are all the same. How, then, do I get the lines to vary for large series counts?
推荐答案
使用 scale_..._manual
的技巧通常是发送一个命名向量作为 value
争论.setNames
函数对此很有用
The trick with using scale_..._manual
is often to send a named vector as the value
argument. The setNames
function is good for this
首先,一些虚拟数据
## 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))
接下来我们创建一个向量,它是从 1:12 开始的随机样本(带替换),并将名称设置为与 geography
变量相同
Next we create a vector that is a random sample from 1:12 (with replacement) and set the names the same as the geography
variable
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
现在您可以将 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)
给你什么
顺便说一句,您真的想将年份绘制为因子变量吗?
As an aside, do you really want to plot the years as a factor variable?
这篇关于将带有线型的变量传递给 ggplot 线型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!