微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带点的堆叠条形图,但具有不同的长度-ggplot2

如何解决带点的堆叠条形图,但具有不同的长度-ggplot2

I have a dataframe which I used the melt function to get to this (length = 118): 

 record_id          value Values
1           8     int_to_out     20
2          14     int_to_out     32
3           5     int_to_out     22
4           6     int_to_out     19
5          31     int_to_out     15
6          48     int_to_out     20
7         100     int_to_out     30
...       ...        ...        ...
113        87 symptom_to_int      7
114        72 symptom_to_int      4
115        99 symptom_to_int      3
116       102 symptom_to_int     36
117       103 symptom_to_int     13
118       111 symptom_to_int      6

我用这个做一个堆积的小图:

enter image description here

该图有59个y元素,我需要根据原始(非熔融)数据向它们添加点。 所以我写了这个:

ggplot(t,aes(y=as.factor(record_id),x=Values,fill=value)) + 
    geom_bar(position=position_stack(reverse= TRUE),stat="identity") +
    geom_point(data = new_df,aes(x=sorolog,y = record_id),colour = "#a81802",size = 4,shape = 1)

x = sorologrecord_id中的59个ID具有59个值。

但是当我运行它时,我得到了:

    Error: Aesthetics must be either length 1 or the same as the data (59): fill
Run `rlang::last_error()` to see where the error occurred.

我认为这与融化的数据有冲突,因为它的长度是原始数据帧的两倍。

问题是:如何添加具有这种不同长度的点?

一个问题:如何在剧情中添加第二个图例?

我使用了以下代码

ggplot() + 
    geom_bar(data=t,fill=value),position=position_stack(reverse= FALSE),stat="identity",width = 0.5) +
        scale_fill_manual(values = c("brown1","chocolate1"),name = "",labels = c("Hospitalization to discharge","Symptom to Hospitalization")) +
    geom_point(data = new_df,y = as.factor(record_id)),colour = "darkcyan",size = 5,shape = 1)+
    geom_point(data = new_df,aes(x=final,colour = "darkred",shape = 16)+

        theme_minimal()+
    labs(title="Patient timeline - from symptoms to hospitalization and discharge",x ="Days",y = "Patient ID")+
    theme(text = element_text(family = "Garamond",color = "grey20"))

得到了:

enter image description here

但是我无法为geom_point元素添加图例,该怎么做?

编辑

通过Dave Armstrong的编辑,我得到了:

enter image description here

解决方法

如果无法访问数据,则必须进行确认,但是如果您从ggplot()中删除数据和美观并将它们放在geom_bar()中,它将可以正常工作:

ggplot() + 
    geom_bar(data=t,aes(y=as.factor(record_id),x=Values,fill=value),position=position_stack(reverse= TRUE),stat="identity") +
    geom_point(data = new_df,aes(x=sorolog,y = record_id),colour = "#a81802",size = 4,shape = 1)

编辑

我正在为有关为这些点添加颜色图例的问题添加答案。还增加了点的大小和形状。

ggplot() + 
  geom_bar(data=t,position=position_stack(reverse= FALSE),stat="identity",width = 0.5) +
  scale_fill_manual(values = c("brown1","chocolate1"),name = "",labels = c("Hospitalization to Discharge","Symptom to Hospitalization")) +
  geom_point(data = new_df,y = as.factor(record_id),colour="Point Label 1",size="Point Label 1",shape="Point Label 1")) +  
  geom_point(data = new_df,aes(x=final,colour="Point Label 2",size="Point Label 2",shape="Point Label 2")) + 
  scale_colour_manual("points",values=c("Point Label 1" = "darkcyan","Point Label 2" = "darkred"),labels= c("Point Label 1","Point Label 2")) + 
  scale_shape_manual("points",values=c("Point Label 1" = 1,"Point Label 2" = 16),"Point Label 2")) + 
  scale_size_manual("points",values=c("Point Label 1" = 5,"Point Label 2" = 4),"Point Label 2")) + 
  theme_minimal()+
  labs(title="Patient timeline - from symptoms to hospitalization and discharge",x ="Days",y = "Patient ID")+
  theme(text = element_text(family = "Garamond",color = "grey20"))

这里的窍门是将所有点属性-颜色,大小和形状都放在带有相同标签的美学中。提供给values的属性本身必须被命名为vector,其名称必须与美学名称相同。我发现this post有助于将各个部分组合在一起。

主要思想是,您必须在这些点上添加色彩美感,但是它不必来自数据框中的变量,您可以随时对其进行弥补。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。