美学的长度必须为1或与数据相同(366):x,y

时间:2019-02-22 22:21:58

标签: r ggplot2

ggplot() +
      # blue plot
      geom_point(data=Cen, aes(x=log(area[MainIsl == "Main"&organism == "Plant"]), y=log(native[MainIsl == "Main"&organism == "Plant"]))) + 
      geom_smooth(data=Cen, aes(x=log(area[MainIsl == "Main"&organism == "Plant"]), y=log(native[MainIsl == "Main"&organism == "Plant"])), fill="blue",colour="darkblue") +
      # red plot
      geom_point(data=Cen, aes(x=log(area[MainIsl == "Main"&organism == "Plant"]), y=log(exotic[MainIsl == "Main"&organism == "Plant"]))) + 
      geom_smooth(data=Cen, aes(x=log(area[MainIsl == "Main"&organism == "Plant"]), y=log(exotic[MainIsl == "Main"&organism == "Plant"])), fill="red",colour="red")

我使用了上面的代码,但是R给了我一个错误。 我拥有的数据集包含以下列:MainIsl(大陆或岛屿),生物(植物或鸟类),本地人数量,外来物种数量,总数和面积。

我想根据其机体和MainIsl绘制针对#种本地人,外来物种和总数的区域。我想在一个图中将属于同一MainIsl和同一生物的点放在一起,用不同的颜色表示该点是显示#native还是外来或总计。

这是使用ggplot实现此想法的正确方法吗?

1 个答案:

答案 0 :(得分:0)

我支持卡米尔在上述评论中所说的一切。这是您可以做什么的示例:

library(tidyverse)

# example data
Cen <- data.frame(MainIsl = sample(c("Mainland", "Island"), 100, replace = T), organism = sample(c("Plant", "Bird"), 100, replace = T),
                  native = sample(1:100, 100, replace = T), exotic = sample(1:100, 100, replace = T), area = sample(1:100, 100, replace = T)
            )

Cen$Total <- Cen$native + Cen$exotic

#  convert to long format
Cen2 <- gather(Cen, key = "Sp", value = "Richness", -MainIsl, -organism, -area)

# plot
ggplot(data = Cen2, aes(x = log(area), y = log(Richness), colour = Sp)) +
  geom_point() +
  geom_smooth()+
  facet_grid(MainIsl~organism)

enter image description here