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

Stacked bar plot with points, but with different aestetics length - 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

我用这个做了一个堆叠的条形图:

该图有 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, aes(y=as.factor(record_id), x=Values, 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, aes(x=sorolog, y = as.factor(record_id)), 
                colour = "darkcyan", size = 5, shape = 1)+
    geom_point(data = new_df, aes(x=final, y = as.factor(record_id)), 
                colour = "darkred", size = 4, 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"))

得到这个:

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

编辑

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

如果您无法访问数据,您必须确认,但如果您从 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, aes(y=as.factor(record_id), x=Values, 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, aes(x=sorolog, 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, y = as.factor(record_id), 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), 
                      labels= c("Point Label 1", "Point Label 2")) + 
  scale_size_manual("points", values=c("Point Label 1" = 5, "Point Label 2" = 4), 
                     labels= c("Point Label 1", "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 的属性本身需要命名为向量,其中名称与美学名称相同。我发现 this post 有助于将各个部分组合在一起。

主要思想是你必须为点添加颜色美感,但它不必来自数据框中的变量,你可以即时弥补。