[英]ggplot2: show legend shape based on graph's type
我有以下小標題數據:
d1<-structure(list(Date = structure(c(18413, 18414, 18415, 18416,
18417, 18418, 18419, 18420, 18421, 18422), class = "Date"), F = c(27240.15794,
28169.30859, 29175.23888, 30136.86232, 31124.0537, 32096.49071,
33077.44196, 34053.47994, 35032.35319, 36009.5903), L95 = c(27075.72202,
27869.77696, 28635.16841, 29389.18107, 30126.3899, 30842.16607,
31542.19984, 32223.65431, 32889.86495, 33539.92631), H95 = c(27404.59386,
28468.84022, 29715.30936, 30884.54358, 32121.71751, 33350.81536,
34612.68407, 35883.30556, 37174.84142, 38479.25429), A = c(27043L,
27762L, 28649L, 29359L, 29921L, 30644L, 31131L, 31848L, 32510L,
33140L)), row.names = c(NA, -10L), class = c("tbl_df", "tbl",
"data.frame"))
這是我的代碼:
colors <- c("A" = "black", "F" = "blue", "B" = "red")
d1 %>%
ggplot(aes(x = Date, y = A, color="A")) +
geom_point(shape=8,fill="blue", size=1.5) +
geom_line() +
geom_point(aes(x=Date, y = F,color="F"),shape=24,size=1.5) +
geom_line() +
geom_ribbon(aes(x = Date, y = F,ymax = L95,ymin = H95,color="B"),
fill = "gray95",
alpha = 0.6,
linetype=6,
show.legend = TRUE)+
theme_bw() +
theme(legend.position = "bottom",
#legend.box = "vertical",
legend.title=element_blank(),
legend.box = NULL,
legend.box.background = element_blank(),
#legend.background = element_blank(),
legend.key.size = unit(0.5, "cm"),
legend.key.width = unit(0.5,"cm"),
legend.direction = "horizontal",
#legend.background = element_rect(color = "steelblue", linetype = "solid"),
axis.text.x = element_text(color="#000000",
size=10,
angle=45,
hjust = 1),
axis.text.y = element_text(color="#000000",
size=10,
angle=45,
hjust = 1),
axis.title.x =element_blank(),
axis.title.y=element_text(size=14),
panel.border = element_blank(),
#panel.grid.major = element_blank(),
panel.background = element_rect(fill="#FFFFFF"),
axis.line = element_line(colour = "black",
size = 0.5,
linetype = "solid"),
#legend.position = c(0, 160000),legend.justification = c(0, 1)
) +
#scale_shape_manual(values = colors)+
scale_color_manual(values = colors)+
scale_y_continuous("Test",expand = expansion(mult = c(0.01,0.03))) +
scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week",
date_labels = "%b/%d",
expand = expansion(mult = c(0.01,0.03)))
output:
我想將圖例的形狀與圖中的形狀相匹配,並去掉每個圖例周圍的小框。 此外,由於某種原因,在我使用了一些推薦的圖例自定義命令后, F數據的連接線變為僅點。
我從前面的問題中找到了一些融化數據的建議(使用reshape2包)。 我確實嘗試過,但我無法獲得相同的帶狀圖形效果,尤其是 L95 和 H95 之間的填充區域。 因此,我消除了這個建議。 另一個建議是創建一個形狀列表作為與我的代碼相同的變量(用於顏色),然后使用scale_shape_manual
。 我試過了,但結果沒有任何變化。 我不確定我錯過了什么。 有什么建議么?
嘗試將此數據重塑為 long 以避免潛在的陷阱:
library(dplyr)
library(tidyr)
library(ggplot2)
#Inputs
colors <- c("A" = "black", "F" = "blue", "B" = "red")
#Code
d1 %>%
pivot_longer(-c(Date,L95,H95)) %>%
ggplot(aes(x = Date, y = value, color=name)) +
geom_point(aes(shape=name), size=1.5) +
geom_line()+
geom_ribbon(aes(x = Date,ymax = L95,ymin = H95,color="B",shape='B'),
fill = "gray95",
alpha = 0.6,
linetype=6,
show.legend = F)+
scale_shape_manual('Var',values = c('A'=8,'F'=24,'B'=1))+
scale_color_manual('Var',values = colors)+
scale_y_continuous("Test",expand = expansion(mult = c(0.01,0.03))) +
scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week",
date_labels = "%b/%d",
expand = expansion(mult = c(0.01,0.03)))+
theme_bw() +
theme(legend.position = "bottom",
legend.title=element_blank(),
legend.box = NULL,
legend.box.background = element_blank(),
legend.key.size = unit(0.5, "cm"),
legend.key.width = unit(0.5,"cm"),
legend.direction = "horizontal",
axis.text.x = element_text(color="#000000",
size=10,
angle=45,
hjust = 1),
axis.text.y = element_text(color="#000000",
size=10,
angle=45,
hjust = 1),
axis.title.x =element_blank(),
axis.title.y=element_text(size=14),
panel.border = element_blank(),
panel.background = element_rect(fill="#FFFFFF"),
axis.line = element_line(colour = "black",
size = 0.5,
linetype = "solid"),
legend.key = element_rect(colour = NA, fill = NA)
)+
guides(color = guide_legend(override.aes = list(shape = c(8,NA,24))))
Output:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.