如何在RStudio

时间:2018-03-23 05:42:59

标签: r ggplot2

我需要帮助:

我似乎无法为ggplot R STDUIO的以下代码添加图例

ggplot(Report_Data, 
   aes(x=Report_Data$Transect Point), show.legend = TRUE) + 
geom_point(aes(y=Report_Data$Q1North), 
   shape = 6, size = 5, colour = label , show.legend = TRUE) + 
geom_point(aes(y=Report_Data$Q1South), 
   shape = 4, size = 5, colour = label, show.legend = TRUE)+ 
labs(title="Density of Trees Species found North & South of the creek using two sampling methods",
   y="Density in Tree Species Found", x="Transect Points",caption = "n7180853")+
geom_line(aes(y=Report_Data$Q1North, colour = Q1North), 
   colour = "green", size = 1, show.legend = TRUE)+  
geom_line(aes(y=Report_Data$Q1South, color = Q1South), 
   colour = "pink4", size = 1, show.legend = TRUE)+ theme(legend.position = "right")

1 个答案:

答案 0 :(得分:0)

如果没有您的数据,很难复制,但如果您希望您的绘图具有点形状的图例,则需要将其包含在该图层的aes(...)内。如果您愿意,可以与颜色或大小相似。然后,根据需要添加scale_shape_manual(...)scale_colour_manual(...),指定您的具体值。

以下是使用默认diamonds数据集的玩具示例。

data("diamonds")

ggplot(diamonds, aes(x = carat)) +
  geom_point(data = diamonds[diamonds$cut == "Ideal",], 
             aes(y= price, shape = cut)) +
  geom_point(data = diamonds[diamonds$cut == "Good",],
             aes(y = price, shape = cut)) +
  scale_shape_manual(values = c(6,4))

enter image description here