r - 将带有线型的变量传递给 ggplot 线型

标签 r charts ggplot2

我是 ggplot 的新手,所以请耐心等待。我正在绘制 35 个小区域地理的增长预测,即使使用了很棒的 directlabels 对于一个地块来说,这是一个不健康的数量。图书馆。但是我需要所有系列进行初步筛选。

挑战在于使其具有可读性。我找到了@Ben Bolker 的修复程序,用于使用 large numbers of distinct colors但是在改变线型时遇到了麻烦。 35 系列不需要是唯一的,但我想使用 12 种不同的类型来使单个系列更易于阅读。

我的计划是创建一个包含 12 种可能类型的 35 个元素的随机列表,并将其作为 linetype 参数传递,但我无法使其正常工作,并出现错误:

Error: Aesthetics must either be length one, or the same length as the dataProblems:lty

线型列表中有 35 个值。当然,我希望类型、颜色和所有内容都反射(reflect)在图例中。

融化的数据看起来像这样; 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    

到目前为止,这是我的代码:
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 参数生成图表之后,但线条都是相同的。那么,如何让线条因大系列计数而变化?

example plot

最佳答案

使用技巧 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))

接下来,我们创建一个向量,它是从 1:12(带替换)开始的随机样本,并将名称设置为与 geography 相同的名称。多变的
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)

这给你

enter image description here

顺便说一句,您真的想将年份绘制为因子变量吗?

关于r - 将带有线型的变量传递给 ggplot 线型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11714564/

相关文章:

r - [R} 使用 for 循环对元素进行排序时追加的问题

r - 如何使 ggplot2 barplot 中的条形具有 Shiny 的交互性?

regex - 期间前删除所有内容

r - 自动折叠 R Bookdown 网站的侧边栏

java - 如何使用 MPAndroid 图表库跳过折线图中特定索引的值?

excel - 如何排列数据透视图中的数据? (堆积柱形图)

r - 单个变量前面的波形符 (~) 是什么意思 (facet_wrap)?

r - 如何在分组密度图上添加中值?

r - `stat_smooth()` : object 'C_crspl' not found 计算失败

javascript - Angular 的 Highchart 漏斗