简体   繁体   English

在 R 的 ggplot 2 中使用更多数据帧

[英]using more dataframes in ggplot 2 in R

I get an error when i try to use more than one dataframe in a ggplot2;当我尝试在 ggplot2 中使用多个 dataframe 时出现错误; Error: mapping must be created by aes()错误: mapping必须由aes()创建

xdf <- data.frame(x=1:3, y=c(18,11,16))
ydf <- data.frame(x=c(5,7), y=c(18,11))

ggplot(xdf, aes(x,y))+ 
  geom_point()+
  geom_point(ydf, aes(x,y) )

Is this solvable...?这可以解决吗...?

You need to specify that ydf is the data parameter:您需要指定ydfdata参数:

ggplot(xdf, aes(x,y))+ 
  geom_point()+
  geom_point(data=ydf, aes(x,y))

Some additional remarks for others who need to work with different data in ggplot2 :对于需要在ggplot2中处理不同数据的其他人的一些补充说明:

If you define aes globally within the function ggplot and you need the same aesthetics, you do not have to write them again.如果您在 function ggplot中全局定义aes并且您需要相同的美学,则不必再次编写它们。

ggplot(xdf, aes(x, y)) + 
  geom_point() +
  geom_point(data = ydf)

If you need different aesthetics, you can set inherit.aes = FALSE to ignore the global aesthetics.如果需要不同的美学,可以设置inherit.aes = FALSE忽略全局美学。

ggplot(xdf, aes(x, y, color = z)) + 
  geom_point() +
  geom_point(data=ydf, aes(y, x), inherit.aes = FALSE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM