R ggplot 不会显示图例颜色
R ggplot won't show legend colors
我正在尝试用 geom_linerange() 绘制一个图,aes(color) 由一个变量给出。
我不明白为什么它在图例框中不显示颜色。
这是我的剧情代码:
estancias_uci %>%
arrange(desc(box),desc(first_cauris)) %>%
rowid_to_column("patient_id") %>%
ggplot(data=.,
aes(x=nhc)) +
geom_linerange(
aes(ymin=first_cauris-hours(6),ymax=last_cauris+hours(6)),
colour="grey",
size=4,alpha=0.5) +
geom_linerange(aes(ymin=from,ymax=to,color=box), size=2, alpha=0.9) +
coord_flip() +
xlab("") +
ggtitle("UCI 2021") +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
legend.title.align = 0.5)
谢谢
可重现代码
estancias_uci <- structure(list(nhc = c(1057591, 1867803, 1867803, 231669, 231669,
231669, 2829584, 2829584, 2877093, 2877093, 2877093, 2935589,
2935589, 3078408, 3188088, 3188088, 3193727, 3193727, 3193727,
6080761, 6787731, 6787731, 6787731), from = structure(c(1610125680,
1611092580, 1615554240, 1611701220, 1614024120, 1616442960, 1612849620,
1614024120, 1610797440, 1610797620, 1614249780, 1615318260, 1616508540,
1614952440, 1615236240, 1615395120, 1610057340, 1612637100, 1614171300,
1612305960, 1613691540, 1614372060, 1620653340), tzone = "UTC", class = c("POSIXct",
"POSIXt")), to = structure(c(1616187780, 1615554240, 1617456120,
1614024120, 1616442960, 1617471240, 1614024120, 1615053600, 1610797620,
1614249780, 1614622320, 1616508540, 1621436460, 1616434140, 1615395120,
1615817940, 1612637100, 1614171300, 1616434560, 1613989800, 1614372060,
1620653340, 1622131020), tzone = "UTC", class = c("POSIXct",
"POSIXt")), box = c("BOX-1", "BOX-1", "BOX-1", "BOX-1", "BOX-1",
"BOX-1", "BOX-1", "BOX-1", "BOX-1", "BOX-1", "BOX-1", "BOX-1",
"BOX-1", "BOX-1", "BOX-1", "BOX-1", "BOX-3", "BOX-3", "BOX-1",
"BOX-1", "BOX-1", "BOX-1", "BOX-1"), first_cauris = structure(c(1613347200,
1615161600, 1615161600, 1613606400, 1613606400, 1613606400, 1613606400,
1613606400, 1614211200, 1614211200, 1614211200, 1617667200, 1617667200,
1616371200, 1615766400, 1615766400, 1615161600, 1615161600, 1615161600,
1612742400, 1613952000, 1613952000, 1613952000), tzone = "UTC", class = c("POSIXct",
"POSIXt")), last_cauris = structure(c(1615939200, 1618012800,
1618012800, 1617667200, 1617667200, 1617667200, 1614816000, 1614816000,
1615161600, 1615161600, 1615161600, 1618444800, 1618444800, 1617062400,
1616976000, 1616976000, 1618272000, 1618272000, 1618272000, 1613606400,
1618444800, 1618444800, 1618444800), tzone = "UTC", class = c("POSIXct",
"POSIXt"))), row.names = c(NA, -23L), class = c("tbl_df", "tbl",
"data.frame"))
问题似乎是您正在为这些颜色设置 alpha,但您不想在图例中使用 alpha。尝试添加
guides(color=guide_legend(override.aes = list(alpha=1)))
你的阴谋。这解决了我的问题。
我正在尝试用 geom_linerange() 绘制一个图,aes(color) 由一个变量给出。
我不明白为什么它在图例框中不显示颜色。
这是我的剧情代码:
estancias_uci %>%
arrange(desc(box),desc(first_cauris)) %>%
rowid_to_column("patient_id") %>%
ggplot(data=.,
aes(x=nhc)) +
geom_linerange(
aes(ymin=first_cauris-hours(6),ymax=last_cauris+hours(6)),
colour="grey",
size=4,alpha=0.5) +
geom_linerange(aes(ymin=from,ymax=to,color=box), size=2, alpha=0.9) +
coord_flip() +
xlab("") +
ggtitle("UCI 2021") +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
legend.title.align = 0.5)
谢谢
可重现代码
estancias_uci <- structure(list(nhc = c(1057591, 1867803, 1867803, 231669, 231669,
231669, 2829584, 2829584, 2877093, 2877093, 2877093, 2935589,
2935589, 3078408, 3188088, 3188088, 3193727, 3193727, 3193727,
6080761, 6787731, 6787731, 6787731), from = structure(c(1610125680,
1611092580, 1615554240, 1611701220, 1614024120, 1616442960, 1612849620,
1614024120, 1610797440, 1610797620, 1614249780, 1615318260, 1616508540,
1614952440, 1615236240, 1615395120, 1610057340, 1612637100, 1614171300,
1612305960, 1613691540, 1614372060, 1620653340), tzone = "UTC", class = c("POSIXct",
"POSIXt")), to = structure(c(1616187780, 1615554240, 1617456120,
1614024120, 1616442960, 1617471240, 1614024120, 1615053600, 1610797620,
1614249780, 1614622320, 1616508540, 1621436460, 1616434140, 1615395120,
1615817940, 1612637100, 1614171300, 1616434560, 1613989800, 1614372060,
1620653340, 1622131020), tzone = "UTC", class = c("POSIXct",
"POSIXt")), box = c("BOX-1", "BOX-1", "BOX-1", "BOX-1", "BOX-1",
"BOX-1", "BOX-1", "BOX-1", "BOX-1", "BOX-1", "BOX-1", "BOX-1",
"BOX-1", "BOX-1", "BOX-1", "BOX-1", "BOX-3", "BOX-3", "BOX-1",
"BOX-1", "BOX-1", "BOX-1", "BOX-1"), first_cauris = structure(c(1613347200,
1615161600, 1615161600, 1613606400, 1613606400, 1613606400, 1613606400,
1613606400, 1614211200, 1614211200, 1614211200, 1617667200, 1617667200,
1616371200, 1615766400, 1615766400, 1615161600, 1615161600, 1615161600,
1612742400, 1613952000, 1613952000, 1613952000), tzone = "UTC", class = c("POSIXct",
"POSIXt")), last_cauris = structure(c(1615939200, 1618012800,
1618012800, 1617667200, 1617667200, 1617667200, 1614816000, 1614816000,
1615161600, 1615161600, 1615161600, 1618444800, 1618444800, 1617062400,
1616976000, 1616976000, 1618272000, 1618272000, 1618272000, 1613606400,
1618444800, 1618444800, 1618444800), tzone = "UTC", class = c("POSIXct",
"POSIXt"))), row.names = c(NA, -23L), class = c("tbl_df", "tbl",
"data.frame"))
问题似乎是您正在为这些颜色设置 alpha,但您不想在图例中使用 alpha。尝试添加
guides(color=guide_legend(override.aes = list(alpha=1)))
你的阴谋。这解决了我的问题。