I have a plot with some points and I'd like to use segment to connect them
我有一些积分的情节,我想用段来连接它们
dummy = data.frame(GROUP=c("A","B","C","D"),
X = c(80,75,68,78),
Y=c(30, 32,36,33)
)
df= data.frame(x1 = c(80), x2 =c(78) , y1=c(30), y2 =c(33))
df
library(ggplot2)
ggplot(dummy,aes(x=X,y=Y,color=GROUP)) +
geom_point() +
geom_segment(aes(x=x1,y=y1,xend= x2, yend =y2), data = df)
but I get this error
但是我得到了这个错误
Error in eval(expr, envir, enclos) : object 'GROUP' not found
What am I doing wrong here?
我在这做错了什么?
1 个解决方案
#1
13
Aesthetic mapping defined in the initial ggplot
call will be inherited by all layers. Since you initialized your plot with color = GROUP
, ggplot
will look for a GROUP
column in subsequent layers and throw an error if it's not present. There are 3 good options to straighten this out:
初始ggplot调用中定义的美学映射将由所有层继承。由于您使用color = GROUP初始化了绘图,因此ggplot将在后续图层中查找GROUP列,如果不存在则抛出错误。有3个很好的选择来理顺这个:
Option 1: Set inherit.aes = F
in the layer the you do not want to inherit aesthetics. Most of the time this is the best choice.
选项1:在图层中设置inherit.aes = F,您不想继承美学。大多数时候这是最好的选择。
ggplot(dummy,aes(x = X, y = Y, color = GROUP)) +
geom_point() +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2),
data = df,
inherit.aes = FALSE)
Option 2: Only specify aesthetics that you want to be inherited (or that you will overwrite) in the top call - set other aesthetics at the layer level:
选项2:仅在顶部调用中指定要继承(或将覆盖)的美学 - 在图层级别设置其他美学:
ggplot(dummy,aes(x = X, y = Y)) +
geom_point(aes(color = GROUP)) +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2),
data = df)
Option 3: Specifically NULL
aesthetics on layers when they don't apply.
选项3:当不适用时,层上的NULL美学。
ggplot(dummy,aes(x = X, y = Y, color = GROUP)) +
geom_point() +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, color = NULL),
data = df)
Which to use?
Most of the time option 1 is just fine. It can be annoying, however, if you want some aesthetics to be inherited by a layer and you only want to modify one or two. Maybe you are adding some errorbars to a plot and using the same x
and color
column names in your main data and your errorbar data, but your errorbar data doesn't have a y
column. This is a good time to use Option 2 or Option 3 to avoid repeating the x
and color
mappings.)
大部分时间选项1都很好。但是,如果你想让一些美学被一个层继承而你只想修改一个或两个,那就太烦人了。也许您正在为绘图添加一些错误栏,并在主数据和错误栏数据中使用相同的x和颜色列名称,但您的错误栏数据没有y列。现在是使用选项2或选项3避免重复x和颜色映射的好时机。)
#1
13
Aesthetic mapping defined in the initial ggplot
call will be inherited by all layers. Since you initialized your plot with color = GROUP
, ggplot
will look for a GROUP
column in subsequent layers and throw an error if it's not present. There are 3 good options to straighten this out:
初始ggplot调用中定义的美学映射将由所有层继承。由于您使用color = GROUP初始化了绘图,因此ggplot将在后续图层中查找GROUP列,如果不存在则抛出错误。有3个很好的选择来理顺这个:
Option 1: Set inherit.aes = F
in the layer the you do not want to inherit aesthetics. Most of the time this is the best choice.
选项1:在图层中设置inherit.aes = F,您不想继承美学。大多数时候这是最好的选择。
ggplot(dummy,aes(x = X, y = Y, color = GROUP)) +
geom_point() +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2),
data = df,
inherit.aes = FALSE)
Option 2: Only specify aesthetics that you want to be inherited (or that you will overwrite) in the top call - set other aesthetics at the layer level:
选项2:仅在顶部调用中指定要继承(或将覆盖)的美学 - 在图层级别设置其他美学:
ggplot(dummy,aes(x = X, y = Y)) +
geom_point(aes(color = GROUP)) +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2),
data = df)
Option 3: Specifically NULL
aesthetics on layers when they don't apply.
选项3:当不适用时,层上的NULL美学。
ggplot(dummy,aes(x = X, y = Y, color = GROUP)) +
geom_point() +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, color = NULL),
data = df)
Which to use?
Most of the time option 1 is just fine. It can be annoying, however, if you want some aesthetics to be inherited by a layer and you only want to modify one or two. Maybe you are adding some errorbars to a plot and using the same x
and color
column names in your main data and your errorbar data, but your errorbar data doesn't have a y
column. This is a good time to use Option 2 or Option 3 to avoid repeating the x
and color
mappings.)
大部分时间选项1都很好。但是,如果你想让一些美学被一个层继承而你只想修改一个或两个,那就太烦人了。也许您正在为绘图添加一些错误栏,并在主数据和错误栏数据中使用相同的x和颜色列名称,但您的错误栏数据没有y列。现在是使用选项2或选项3避免重复x和颜色映射的好时机。)