我不是指实线、点线、虚线等标准选项。我想创建自定义线型,如(..--..--..)和(-...-...-)。
请找到我到现在为止开发的代码和我在下面生成的图表。请注意,我有10个子数据集,用于生成组合折线图。
ggplot() +
geom_line(data = datav2_MH, aes(x = date, y = MH, linetype = "MH"),
colour = "black", size = 1, group = 1) +
geom_line(data = datav2_AP, aes(x = date, y = AP, linetype = "AP"),
colour = "black", size = 1, group = 1) +
geom_line(data = datav2_TN, aes(x = date, y = TN, linetype = "TN"),
colour = "black", size = 1, group = 1) +
geom_line(data = datav2_KN, aes(x = date, y = KN, linetype = "KN"),
colour = "black", size = 1, group = 1) +
geom_line(data = datav2_UP, aes(x = date, y = UP, linetype = "UP"),
colour = "black", size = 1, group = 1) +
geom_line(data = datav2_DL, aes(x = date, y = DL, linetype = "DL"),
colour = "grey", size = 1, group = 1) +
geom_line(data = datav2_WB, aes(x = date, y = WB, linetype = "WB"),
colour = "grey", size = 1, group = 1) +
geom_line(data = datav2_KR, aes(x = date, y = KR, linetype = "KR"),
colour = "grey", size = 1, group = 1) +
geom_line(data = datav2_OD, aes(x = date, y = OD, linetype = "OD"),
colour = "grey", size = 1, group = 1) +
geom_line(data = datav2_RJ, aes(x = date, y = RJ, linetype = "RJ"),
colour = "grey", size = 1, group = 1) +
guides(fill = guide_legend(keywidth = 1, keyheight = 1),
linetype=guide_legend(keywidth = 2.5, keyheight = 1))+
scale_linetype_manual(values = c(
'MH' = 1, "AP" = 2, "TN" = 3,
"KN" = 4, 'UP' = 5, "DL" = 6,
"WB" = 1, 'KR' = 2,
"OD" = 3, "RJ" = 4))+
geom_dl(data = datav2_MH, aes(x = date, y = MH, label = "MH"), method = "top.qp", cex = 0.2) +
geom_dl(data = datav2_AP, aes(x = date, y = AP, label = "AP"), method = "top.qp", cex = 0.2) +
geom_dl(data = datav2_TN, aes(x = date, y = TN, label = "TN"), method = "top.qp", cex = 0.2) +
geom_dl(data = datav2_KN, aes(x = date, y = KN, label = "KN"), method = "top.qp", cex = 0.2) +
geom_dl(data = datav2_DL, aes(x = date, y = DL, label = "DL"), method = "top.qp", cex = 0.2) +
geom_dl(data = datav2_UP, aes(x = date, y = UP, label = "UP"), method = "top.qp", cex = 0.2) +
geom_dl(data = datav2_WB, aes(x = date, y = WB, label = "WB"), method = "top.qp", cex = 0.2) +
geom_dl(data = datav2_KR, aes(x = date, y = KR, label = "KR"), method = "top.qp", cex = 0.2) +
geom_dl(data = datav2_OD, aes(x = date, y = OD, label = "OD"), method = "top.qp", cex = 0.2) +
geom_dl(data = datav2_RJ, aes(x = date, y = RJ, label = "RJ"), method = "top.qp", cex = 0.2) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "top") +
theme(plot.margin = unit(c(1,1,1,1),"cm")) +
xlab("Date") +
ylab("Total_Confirmed_Cases") +
scale_y_continuous() +
scale_x_discrete(breaks = c("2020-03-31", "2020-04-30", "2020-05-31", "2020-06-30",
"2020-07-31", "2020-08-31", "2020-09-30", "2020-10-31", "2020-11-30", "2020-12-31",
"2021-01-31")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
The chart containing a line for 10 states. The data is at a daily level
发布于 2021-02-26 01:24:02
如果要手动指定线型,可以使用scale_linetype_manual()
执行此操作。对于values
参数,您可以给出一个包含偶数个(十六进制)数字的字符向量。第一个数字表示短划线长度,第二个数字表示间隙长度。例如,"1234"
将给出一个大小为1的虚线、一个大小为2的间隙、一个大小为3的虚线和一个大小为4的间隙,这将重复出现。为简洁起见,我在下面的示例中使用了不同的数据。
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.3
df <- DNase[!duplicated(DNase[, c("Run", "conc")]),]
ggplot(df, aes(conc, density, linetype = Run)) +
geom_line() +
scale_linetype_manual(values = c(
"12", "34", "41", "F2", "11", "21",
"1141", "2134", "7171", "1717", "4243"
))
https://stackoverflow.com/questions/66373209
复制相似问题