r语言 - 将图形叠加到一个图形中



我想将两个图形合并为一个图形:当我尝试下面的代码时:

data_df <- df%>%
   filter(!is.na(LayerName)) %>%
   dplyr::select(LayerName, A,B) %>%
   group_by(LayerName) %>%
   dplyr::summarise(lower=min(A),upper=max(A),Mean=mean(A),
                lower_out=min(B),upper_out=max(B),Mean_out=mean(B)) 
  c<-ggplot(data = data_df, mapping = aes(x = LayerName, y = Mean)) +
   geom_pointrange(mapping = aes(ymin = lower, ymax = upper),color = "red")

   r<-c+ggplot(data = data_df, mapping = aes(x = LayerName, y = Mean_out)) +
   geom_pointrange(mapping = aes(ymin = lower_out, ymax = upper_out),color = "blue")+
   theme_bw()
   ggplotly(r)

我收到此消息错误:

Don't know how to add ggplot(data = data_df, mapping = aes(x = LayerName, y = Mean_out)) to a plot

您可以添加额外的图层,但要避免使用两次ggplot()。因此,像这样更改 r 的分配可能会起作用:

   r<-c +
   geom_pointrange(mapping = aes(y = Mean_out, ymin = lower_out, ymax = upper_out), color = "blue") +
   theme_bw()

最新更新