使用ggplot aes_string,组和线型时出现问题 [英] Problems when using ggplot aes_string, group, and linetype
问题描述
pre $
x < - rnorm(1000)
y< - rnorm(1000,2,5)
line.color< - sample(rep(1:4,250))
line.type< - as.factor(sample(rep(1:5 ,200)))
data < - data.frame(x,y,line.color,line.type)
我想通过line.type和line.color的交互来绘制x和y变量组。另外我想用line.type指定线型,用line.color指定颜色。如果我写这个:
pre $ g $ p $ ggplot(data,aes(x = x,y = y,group = interaction(line.type ,line.color),color = line.color,linetype = line.type))+ geom_line()
<
与< - c(line.color, line.type)
inter < - paste0(interaction(,paste0(''',interact,'',collapse =,),))
ggplot(data,aes_string(x =x,y =y,group = inter,color =line.color,linetype =line.type))+ geom_line()
我收到错误消息:
错误:geom_path:如果您使用的是虚线或虚线,则颜色,大小和线型必须在行上保持不变。
我做错了什么?我需要使用aes_string,因为我有很多要绘制的变量。
您几乎定义了
inter < - paste0(interaction(,paste0('''',interact,''',collapse =,), ))
然而,对于 aes_string
到你需要传递一个字符串,如果你调用的是 aes
,那么你不需要在交互
作为字符串。你想创建一个字符串交互(line.color,line.type)
。因此
inter < - paste0('interaction(',paste0(interact,collapse =','),')' )
#或
#inter < - sprintf('interaction(%s),paste0(interact,collapse =','))
#结果是
inter
## [1]interaction(line.color,line.type)
#和以下作品
ggplot(data,aes_string(x =x,y =y,
group = inter,color =line.color,linetype =line.type))+
geom_line()
Let's say I have this data set:
x <- rnorm(1000)
y <- rnorm(1000, 2, 5)
line.color <- sample(rep(1:4, 250))
line.type <- as.factor(sample(rep(1:5, 200)))
data <- data.frame(x, y, line.color, line.type)
I'm trying to plot the x and y variables group by the interaction of line.type and line.color. In addition I want to specify the linetype using line.type and the color using line.color. If I write this:
ggplot(data, aes(x = x, y = y, group = interaction(line.type, line.color), colour = line.color, linetype = line.type)) + geom_line()
It works but If I try to use aes_string like this:
interact <- c("line.color", "line.type")
inter <- paste0("interaction(", paste0('"', interact, '"', collapse = ", "), ")")
ggplot(data, aes_string(x = "x", y = "y", group = inter, colour = "line.color", linetype = "line.type")) + geom_line()
I get the error:
Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
What am I doing wrong? I need to use aes_string because I have a lot of variables to plot.
You were almost there defining
inter <- paste0("interaction(", paste0('"', interact, '"', collapse = ", "), ")")
However, for aes_string
to work, you need pass a character string of what would work if it you were calling aes
, that is you don't need to have the arguments within interaction
as strings. You want to create a string "interaction(line.color, line.type)"
. Therefore
inter <- paste0('interaction(', paste0(interact, collapse = ', ' ),')')
# or
# inter <- sprintf('interaction(%s), paste0(interact, collapse = ', '))
# the result being
inter
## [1] "interaction(line.color, line.type)"
# and the following works
ggplot(data, aes_string(x = "x", y = "y",
group = inter, colour = "line.color", linetype = "line.type")) +
geom_line()
这篇关于使用ggplot aes_string,组和线型时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!