gpt4 book ai didi

r - 以尺寸图例 geom_point ggplot2 显示所有形状

转载 作者:行者123 更新时间:2023-12-01 03:12:07 24 4
gpt4 key购买 nike

我的图中使用了 3 个形状,size legend 中仅使用了实心圆(如图所示)。我试图找到一种方法来包含该图例中的所有形状,有没有办法做到这一点?

数据框和绘图代码:

data <- structure(list(date = structure(c(1431388800, 1444780800, 1456876800, 
1469145600, 1469664000, 1425081600, 1445299200, 1488758400, 1524960000,
1454544000, 1540512000, 1429228800, 1481587200, 1526688000, 1445904000,
1498348800, 1464825600, 1451174400, 1450310400, 1539216000, 1528934400,
1525996800, 1465084800, 1531180800), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), cnt1 = c(76, 140, 99, 10, 123, 124, 160, 58,
47, 43, 86, 112, 26, 123, 111, 49, 65, 93, 174, 49, 5, 16, 84,
4), cnt2 = c(111, 200, 550, 73, 123, 184, 166, 131, 189, 136,
250, 195, 26, 154, 125, 100, 168, 151, 255, 49, 200, 144, 177,
4), per = c(68.4684684684685, 70, 18, 13.6986301369863, 100,
67.3913043478261, 96.3855421686747, 44.2748091603053, 24.8677248677249,
31.6176470588235, 34.4, 57.4358974358974, 100, 79.8701298701299,
88.8, 49, 38.6904761904762, 61.5894039735099, 68.2352941176471,
100, 2.5, 11.1111111111111, 47.4576271186441, 100), status = c("A",
"B", "C", "C", "B", "B", "B", "A", "A", "A", "C", "C", "C", "A",
"B", "C", "B", "C", "A", "A", "A", "B", "B", "B"), category = c("CAT3",
"CAT1", "CAT3", "CAT2", "CAT1", "CAT1", "CAT1", "CAT3", "CAT2",
"CAT3", "CAT2", "CAT1", "CAT1", "CAT1", "CAT2", "CAT2", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT3", "CAT3", "CAT3", "CAT1")), .Names = c("date",
"cnt1", "cnt2", "per", "status", "category"), row.names = c(NA,
-24L), class = c("tbl_df", "tbl", "data.frame"))



ggplot(data, aes(x=date, y=cnt2)) +
scale_y_log10(breaks=c(1,10,100,1000,5000)) +
scale_size_continuous(trans="sqrt",range = c(1, 5),breaks=c(5,10,25,50,75,100)) +
geom_point(aes(col=category, size=per, shape=status), alpha=0.7)

enter image description here

最佳答案

我找到了一种解决方法,因为似乎没有简单的方法可以做到这一点。理想的解决方案是让形状彼此相邻并在图例中提及一次数字而不是重复它。此外,由于 status 的数量不断变化,因此此解决方法可以更加参数化。并显示一个新的形状,所有输入到覆盖函数的向量都可以正确构造:

    l=c(5,10,25,50,75,100)
mybreaks=c(rep(l,3))
myshapes=c(rep(15,6),rep(16,6),rep(17,6))

ggplot(data, aes(x=date, y=cnt2)) +
scale_y_log10(breaks=c(1,10,100,1000,5000)) +
scale_size_continuous(trans="sqrt",range = c(1, 5),breaks=mybreaks) +
geom_point(aes(col=category, size=per, shape=status), alpha=0.7) +
guides(size=guide_legend(ncol=3,override.aes = list( shape =myshapes)))

enter image description here

关于r - 以尺寸图例 geom_point ggplot2 显示所有形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51409699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com