如果您无法提供色彩美学,请手动创建图例 [英] Manually creating a legend when you can't supply a color aesthetic

查看:175
本文介绍了如果您无法提供色彩美学,请手动创建图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图回答



为了区分注意点,添加颜色会很方便,但 geom_dotplot 颜色上的扼流圈并不会堆叠它们:

  ggplot(CTscores.m,aes(x = variable,y = value,fill = initials))+ 
geom_dotplot(binaxis =y,stackdir = up,binwidth = 0.03,color = NA)+
theme_bw()+ coord_flip()



颜色可以使用黑客手动添加,但:

  gg_color_hue < - 函数(n){
hues = seq(15,375,length = n + 1)
hcl(h =色调,l = 65,c = 100)[1:n]
}

cols < - rep(gg_color_hue(6),4)

ggplot (CTscores.m,aes(x = variable,y = value))+
geom_dotplot(binaxis =y,stackdir =up,binwidth = 0.03,fill = cols,color = NA)+
theme_bw()+ coord_flip()



不幸的是,没有传说。最重要的是,我们不能使用 aes(fill =)尝试手动添加图例,因为它会折叠点。有没有什么办法可以添加图例而不使用 aes()

p>在 gtable 包的帮助下,您可以从图中提取图例,但图例无法堆叠点并添加该图例 grid.arrange gridExtra 包到带有彩色ans堆积点的图表,如下所示:

  p1 < -  ggplot(CTscores.m,aes(x = variable,y = value))+ 
geom_dotplot(binaxis =y,stackdir = ),binwidth = 0.03,fill = cols,color = NA)+
coord_flip()+
theme_bw()

p2 < - ggplot(CTscores.m,aes (x = variable,y = value,fill = initials))+
geom_dotplot(binaxis =y,stackdir =up,binwidth = 0.03,color = NA)+
coord_flip()+
theme_bw()

library(gtable)
fill.legend< - gtable_filter(ggplot_gtable(ggplot_build(p2)),guide-box)
legGrob < - grobTree(fill.legend)

library(gridExtra)
gri d.arrange(p1,legGrob,ncol = 2,widths = c(4,1))

其中给出:


In attempting to answer this question, one way to create the desired plot was to use geom_dotplot from ggplot2 as follows:

library(ggplot2)
library(reshape2)

CTscores <- read.csv(text="initials,total,interest,slides,presentation
CU,1.6,1.7,1.5,1.6
DS,1.6,1.7,1.5,1.7
VA,1.7,1.5,1.5,2.1
MB,2.3,2.0,2.1,2.9
HS,1.2,1.3,1.4,1.0
LS,1.8,1.8,1.5,2.0")

CTscores.m = melt(CTscores, id.var="initials")

ggplot(CTscores.m, aes(x=variable, y=value)) +
  geom_dotplot(binaxis="y", stackdir="up",binwidth=0.03) +
  theme_bw()+coord_flip()

In order to distinguish the points, it would be convenient to just add color, but geom_dotplot chokes on color and doesn't end up stacking them:

ggplot(CTscores.m, aes(x=variable, y=value, fill=initials)) +
  geom_dotplot(binaxis="y", stackdir="up",binwidth=0.03,color=NA) +
  theme_bw()+coord_flip()

Color can be added manually using a hack, though:

gg_color_hue <- function(n) {
  hues = seq(15, 375, length=n+1)
  hcl(h=hues, l=65, c=100)[1:n]
}

cols <- rep(gg_color_hue(6),4)

ggplot(CTscores.m, aes(x=variable, y=value)) +
  geom_dotplot(binaxis="y", stackdir="up",binwidth=0.03,fill=cols,color=NA) +
  theme_bw()+coord_flip()

Unfortunately, there's no legend. On top of that we can't use aes(fill=) to try to add a legend manually because it will collapse the dots. Is there any way to add a legend without using aes()?

解决方案

With the help of the gtable package you can extract the legend from the plot with the legend which fails to stack the dots and add that legend with grid.arrange from the gridExtra package to the plot with the colored ans stacked dots as follows:

p1 <- ggplot(CTscores.m, aes(x=variable, y=value)) +
  geom_dotplot(binaxis="y", stackdir="up", binwidth=0.03, fill=cols, color=NA) +
  coord_flip() +
  theme_bw()

p2 <- ggplot(CTscores.m, aes(x=variable, y=value, fill=initials)) +
  geom_dotplot(binaxis="y", stackdir="up", binwidth=0.03, color=NA) +
  coord_flip() +
  theme_bw()

library(gtable)
fill.legend <- gtable_filter(ggplot_gtable(ggplot_build(p2)), "guide-box") 
legGrob <- grobTree(fill.legend)

library(gridExtra)
grid.arrange(p1, legGrob, ncol=2, widths = c(4,1))

which gives:

这篇关于如果您无法提供色彩美学,请手动创建图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆