[英]matching of shape, color and legend in bubble plot with subset of variable
I have some data我有一些数据
library(data.table)
wide <- data.table(id=c("A","C","B"), var1=c(1,6,1), var2=c(2,6,5), size1=c(11,12,13), size2=c(10,12,10), flag=c(FALSE,TRUE,FALSE))
> wide
id var1 var2 size1 size2 flag
1: A 1 2 11 10 FALSE
2: C 6 6 12 12 TRUE
3: B 1 5 13 10 FALSE
which I would like to plot as bubble plots where id
is ordered by var2
, and bubbles are as follows: ID A and B: var1
is plotted in size1
and "empty bubbles" and var2
is plotted in size2
with "filled" bubbles.我想将 plot 作为气泡图,其中id
由var2
排序,气泡如下: ID A 和 B: var1
绘制在size1
和“空气泡”中, var2
绘制在size2
中并带有“填充”气泡。 ID C is flagged because there is only one value (this is why var1=var2
) and it should have a "filled bubble" of a different color. ID C 被标记是因为只有一个值(这就是为什么var1=var2
)并且它应该有一个不同颜色的“填充气泡”。
I have tried this as follows:我已经尝试如下:
cols <- c("v1"="blue", "v2"="red", "flags"="green")
shapes <- c("v1"=16, "v2"=21, "flags"=16)
p1 <- ggplot(data = wide, aes(x = reorder(id,var2))) + scale_size_continuous(range=c(5,15))
p1 <- p1 + geom_point(aes(size=size1, y = var1, color = "v1", shape = "v1"))
p1 <- p1 + geom_point(aes(size=size2, y = var2, color = "v2", shape = "v2", stroke=1.5))
p1 <- p1 + geom_point(data=subset(wide,flag), aes(size=size2[flag], y=var2[flag], color= "flags", shape="flags"))
p1 <- p1 + scale_color_manual(name = "test",
values = cols,
labels = c("v1", "v2", "flags"))
p1 <- p1 + scale_shape_manual(name = "test",
values = shapes,
labels = c("v1", "v2", "flags"))
which gives (in my theme)这给出了(在我的主题中)
but two questions remain:但还有两个问题:
Overall, something appears to go wrong in matching shape and color.总体而言,go 在匹配形状和颜色方面似乎有些错误。
I admit, it took me a while to understand your slightly convoluted plot.我承认,我花了一段时间才理解你稍微复杂的 plot。 Forgive me, but I have allowed myself to change the way to plot, and make (better?) use of ggplot.原谅我,但我允许自己改变 plot 的方式,并(更好?)使用 ggplot。
The data shape is less than ideal.数据形状不太理想。 ggplot works extremely well with long data. ggplot 非常适合长数据。 It was a bit of a guesswork to reshape your data, and I decided to go the quick and dirty way to simply bind the rows from selected columns.重塑您的数据有点猜测,我决定使用 go 快速而肮脏的方法来简单地绑定选定列中的行。
Now you can see, that you can achieve the new plot with a single call to geom_point
.现在您可以看到,只需调用一次 geom_point 就可以实现新的geom_point
。 The rest is "scale_aesthetic" magic... rest 是“scale_aesthetic”的魔法……
In order to combine the shape and color legend, safest is to use override.aes
.为了结合形状和颜色图例,最安全的是使用override.aes
。 But beware, It does not take named vectors, so the order of the values needs to be in the exact order given by your legend keys - which is usually alphabetic.但请注意,它不采用命名向量,因此值的顺序需要与您的图例键给出的确切顺序一致 - 通常是字母顺序。 if you don't have the factor levels defined.如果您没有定义因子水平。
update re: request to order x labels更新:请求订购 x 个标签
This hugely depends on the actual data structure.这在很大程度上取决于实际的数据结构。 if it is originally as you have presented, I'd first make id
a factor with the levels ordered based on your var2
.如果它最初是您所提出的,我首先将id
设为一个因子,并根据您的var2
排序级别。 Then, do the data shaping.然后,进行数据整形。
library(tidyverse)
# data reshape
wide <- data.frame(id=c("C","B","A"), var1=c(1,6,1), var2=c(2,6,5), size1=c(11,12,13), size2=c(10,12,10), flag=c(FALSE,TRUE,FALSE))
wide <- wide %>% mutate(id = reorder(id, var2))
wide1 <- wide %>% filter(!flag) %>%select(id, var = var1, size = size1)
wide2 <- wide %>% filter(!flag) %>% select(id, var = var2, size = size2)
wide3 <- wide %>% filter(flag) %>% select(id, var = flag, size = size2) %>%
mutate(var = 6)
long <- bind_rows(list(v1 = wide1, v2 = wide2, flag = wide3), .id = "var_id")
# rearrange the vectors for scales aesthetic
cols <- c(flag="green", v1 ="blue", v2="red" )
shapes <- c(flag=16, v1=16, v2 =21 )
ggplot(data = long, aes(x = id, y = var)) +
geom_point(aes(size=size, shape = var_id, color = var_id), stroke=1.5) +
scale_size_continuous(limits = c(5,15),breaks = seq(5,15,5)) +
scale_shape_manual(name = "test", values = shapes) +
scale_color_manual(values = cols, guide = FALSE) +
guides(shape = guide_legend(override.aes = list(color = cols)))
PS the reason for the red stroke around the green bubble in your plot is that you also plotted the 'var2' behind your flag. PS 在您的 plot 中绿色气泡周围出现红色中风的原因是您还在旗帜后面绘制了“var2”。
Created on 2020-04-08 by the reprex package (v0.3.0)由reprex package (v0.3.0) 于 2020 年 4 月 8 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.