gpt4 book ai didi

r - ggplot2 按形状分隔图例

转载 作者:行者123 更新时间:2023-12-05 00:10:02 25 4
gpt4 key购买 nike

# Data:  
zz <- "Small Large Lat Long
1 51 2 11 10
2 49 0 12 11
3 77 7 13 13
4 46 5 12 15
5 32 6 13 14
6 54 3 15 17
7 68 0 14 10
8 39 5 12 13"

Data <- as.data.frame(read.table(text=zz, header = TRUE))

我有一个连续变量,一个比率(小/大),我正在成功绘图。

虽然,“大”变量中存在一些 0。发生这种情况时,我只想绘制“小”数字,因为比率是不可能的。为此,我有以下几点:
ratio.both <- Data %>% 
filter(Large > 0) %>%
mutate(Ratio = Small/Large)

only.sml<- Data %>%
filter(Large < 1)

然后我将两者都绘制在同一张图上(按经纬度数据):
ggplot() +
geom_point(data = ratio.both,
aes(x = Long,
y = Lat,
size = Ratio),
stroke = 0,
colour = '#3B3B3B',
shape=16) +
#
geom_point(data = only.sml,
aes(x = Long,
y = Lat,
size = Small,
shape=1),
stroke = 1,
shape=1)

注意形状的不同。 这绘制了以下内容

not the nicest graph but demonstrates example
不是最好的图表,但演示了示例

比例(填充)和那些只是小值的区别在 map 上很清楚,但在图例中很难区分。

我想要图例中的以下内容:
   #Title
Size = both.ratio$Ratio,
Shape/fill = Ratio or small value #whichever is easier

最佳答案

使用表中的变量通过内置的美学映射来对比数据要容易得多,而不是为小数据和大数据创建单独的几何图形。例如,您可以创建一个新变量来检查该数据点是属于大型还是小型“类型”。然后,您可以映射形状、颜色、大小或任何您想要的美学,并可选择手动添加比例(如果需要)。

Data %>% 
mutate(is_large = ifelse(Large > 0, "Ratio", "Small"),

size = ifelse(is_large == "Large", Small/Large, Small)) %>%
ggplot(aes(Long, Lat,
size = size,
shape = is_large)) +
geom_point() +
scale_shape_manual(values = c("Ratio" = 16, "Small" = 1),
name = "Size") +
scale_size_continuous(name = "Ratio/small value")

enter image description here

或者,如果您想通过点颜色进行对比:
Data %>% 
mutate(is_large = ifelse(Large > 0, "Ratio", "Small"),

size = ifelse(is_large == "Large", Small/Large, Small)) %>%
ggplot(aes(Long, Lat,
size = size,
color = is_large)) +
geom_point() +
scale_color_manual(values = c("Ratio" = "blue", "Small" = "red"),
name = "Size") +
scale_size_continuous(name = "Ratio/small value")

enter image description here

关于r - ggplot2 按形状分隔图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58918527/

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