R ggplot2:基于图形显示图例形状';s型
我有以下tibble数据:R ggplot2:基于图形显示图例形状';s型,r,ggplot2,tidyverse,legend,ribbon,R,Ggplot2,Tidyverse,Legend,Ribbon,我有以下tibble数据: 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
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"))
d1尝试将数据整形为长数据,以避免潜在陷阱:
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))))
库(dplyr)
图书馆(tidyr)
图书馆(GG2)
#投入
颜色%
枢轴长度(-c(日期,L95,H95))%>%
ggplot(aes(x=日期,y=值,颜色=名称))+
几何点(aes(形状=名称),尺寸=1.5)+
geom_线()+
geom_ribbon(aes(x=Date,ymax=L95,ymin=H95,color=“B”,shape='B'),
fill=“gray95”,
α=0.6,
线型=6,
show.legend=F)+
比例-形状-手册('Var',值=c('A'=8,'F'=24,'B'=1))+
比例\颜色\手动(“变量”,值=颜色)+
连续缩放(“测试”,扩展=扩展(mult=c(0.01,0.03)))+
缩放日期(日期间隔=“1周”,日期间隔=“1周”,
日期标签=“%b/%d”,
扩展=扩展(mult=c(0.01,0.03)))+
主题_bw()+
主题(legend.position=“bottom”,
legend.title=element_blank(),
legend.box=NULL,
legend.box.background=元素\空白(),
图例.键.尺寸=单位(0.5,“cm”),
图例.键.宽度=单位(0.5,“cm”),
legend.direction=“水平”,
axis.text.x=element_text(color=“#000000”,
尺寸=10,
角度=45,
hjust=1),
axis.text.y=元素_文本(color=“#000000”,
尺寸=10,
角度=45,
hjust=1),
axis.title.x=元素_blank(),
轴.title.y=元素\文本(大小=14),
panel.border=元素_blank(),
panel.background=element_rect(fill=“#FFFFFF”),
axis.line=元素线(color=“黑色”,
尺寸=0.5,
linetype=“solid”),
图例.键=元素(颜色=NA,填充=NA)
)+
辅助线(颜色=辅助线\图例(override.aes=列表(形状=c(8,NA,24)))
输出:
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))))