将线类型的变量传递到ggplot线型 [英] Passing variable with line types to ggplot linetype

查看:277
本文介绍了将线类型的变量传递到ggplot线型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的ggplot所以忍受我。我正在制定35个小区域地理区域的增长预测,即使使用了奇妙的 directlabels 图书馆,这对一个地区来说是不健康的。但是,我需要所有的系列进行初步筛选。



挑战是让它可读。我发现@Ben Bolker修正了使用大量不同的颜色,但我有麻烦,改变线型。 35系列不需要是唯一的,但我想使用12种不同类型,使单个系列更容易阅读。



我的计划是创建一个随机列表中有35个元素的12种可能的类型,并通过它作为线型的参数,但我有麻烦让它工作,与错误:

 错误:美学必须是长度一或与dataProblems相同的长度:lty 

我在线型列表中有35个值。当然,我想要的类型,颜色和所有都反映在传奇。



熔化的数据看起来像这样; 35个系列中的每个的9年观察:

 > simulation_long_index [16:24,] 
年地理值
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,
color = 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(粘贴(Indexed Value(Rel。to 2012))+
opts(axis.text.x = theme_text(angle = 90,hjust = 0) )

print(g3)

添加

  scale_linetype_manual(,values = lty $ lty)+ 

$ b b

之后,scale_color_manual而不是linetype参数产生图表,但线条都是相同的。那么,我如何得到大系列计数变化的线?



scale _..._ manual

/ code>经常发送一个命名的向量作为 value 参数。 setNames 函数适用于此



首先,一些虚拟数据

  ##一些虚拟数据
模拟< - expand.grid(year = 2012:2020,geography = paste0('a',1:35))
库(plyr)
库(RColorBrewer)
simulation_long_index < - ddply(simulations,。(geography),mutate,
value =(year-2012)* runif -2,2)+ rnorm(9,mean = 0,sd = runif(1,1,3)))
##创建一个manyColors函数
manyColors< - colorRampPalette(brewer.pal name ='Set3',n = 11))

接下来我们创建一个随机样本从1:12(替换)并设置与地理变量相同的名称

  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,
color = 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)

这给你





另外,你真的想把年份作为因子变量吗? / p>

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.

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.

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

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.

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    

Here is my code so far:

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)

adding

    scale_linetype_manual("",values=lty$lty) +

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?

解决方案

The trick with using scale_..._manual is often to send a named vector as the value argument. The setNames function is good for this

First, some dummy data

## 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))

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))

This is what it looks like

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 

Now you can use line_type = geography in conjunction with 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)

Which gives you

As an aside, do you really want to plot the years as a factor variable?

这篇关于将线类型的变量传递到ggplot线型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆