gpt4 book ai didi

r ggplot2 : grouping legend in bar plots

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

我尝试使用 ggplot2 分别使用 fillalpha 来可视化 barplot 中的两个离散变量。标准方法如下:

#creating data and building the basic bar plot
library(ggplot2)
myleg<-read.csv(text="lett,num
a,1
a,2
b,1
b,2
h,1
h,2
h,3
h,4")
ggplot(myleg, aes(lett, alpha = factor(num), fill = lett)) +
geom_bar(position = position_stack(reverse = TRUE)) +
scale_alpha_discrete(range = c(1, .1),
name = "alpha legend",
labels = c("alpha lab 4","alpha lab 3",
"alpha lab 2", "alpha lab 1")) +
labs(title = "initial bar plot for data")

initial bar plot of data

默认图例根据两种不同的呈现方式进行分组(lett 和灰度的着色,或 num 的不透明度)。

我需要将图例分组为绘图上的数据条。即三个颜色条,每个颜色条都有变化的 alpha 级别。部分解决方案是分别生成具有所需三个图例条的绘图,如下所示:

ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position="stack",fill="#f8766d") +scale_alpha_discrete(name="red legend",labels=c("red lab 2","red lab 1"),breaks=c("3","4"))
ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position="stack",fill="#00ba38") +scale_alpha_discrete(name="green legend",labels=c("green lab 2","green lab 1"),breaks=c("3","4"))
ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position="stack",fill="#619cff") +scale_alpha_discrete(name="blue legend",labels=c("blue lab 4","blue lab 3","blue lab 2", "blue lab 1"))

输出为: artificially created plots with needed legend strips

所以现在我只能将三个图例条剪切并粘贴到主图表上(例如在 inkscape 中),以产生所需的结果:

final plot with desired legend

如何以合适的方式对其进行编程?

最佳答案

您可以使用scale_fill_manual绘制自定义图例:

df <- mtcars
df$cyl <- factor(df$cyl)
df$vs <- factor(df$vs)

library(ggplot2)
p <- ggplot(df,aes(x=cyl, fill=interaction(cyl,vs))) + geom_bar(position="stack")

# Breaks
brks <- levels(interaction(df$cyl,df$vs))

# Values - Colors
library(scales)
pal <- hue_pal()(3)
cls <- as.character(c(sapply(pal,alpha,0.3),sapply(pal,alpha,1)))

# Labels
lbls <- paste(levels(df$cyl), "-", rep(levels(df$vs),each=3))

p + scale_fill_manual(name ='Cyl - Vs', breaks=brks, values=cls, labels=lbls)

enter image description here

编辑这是@astrsk发布的(新)问题的解决方案(在他/她更改了最初的问题之后)。

library(ggplot2)
library(grid)
library(gridExtra)
myleg <- structure(list(lett = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 3L,
3L), .Label = c("a", "b", "h"), class = "factor"), num = c(1L,
2L, 1L, 2L, 1L, 2L, 3L, 4L)), .Names = c("lett", "num"),
class = "data.frame", row.names = c(NA, -8L))

getLegend <- function(p) {
g <- ggplotGrob(p)
k <- which(g$layout$name=="guide-box")
g$grobs[[k]]
}

p1 <- ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position="stack",fill="#f8766d") +scale_alpha_discrete(name="red legend",labels=c("red lab 2","red lab 1"),breaks=c("3","4"))
p2 <- ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position="stack",fill="#00ba38") +scale_alpha_discrete(name="green legend",labels=c("green lab 2","green lab 1"),breaks=c("3","4"))
p3 <- ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position="stack",fill="#619cff") +scale_alpha_discrete(name="blue legend",labels=c("blue lab 4","blue lab 3","blue lab 2", "blue lab 1"))

p <- ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +
geom_bar(position=position_stack(reverse=T)) +
scale_alpha_discrete(range=c(1,.1), name="alpha legend",
labels=c("alpha lab 4","alpha lab 3","alpha lab 2", "alpha lab 1")) +
labs(title="initial bar plot for data")
g <- ggplotGrob(p)

k <- which(g$layout$name=="guide-box")
g$grobs[[k]] <- grid.arrange(getLegend(p1),getLegend(p2),getLegend(p3),ncol=1)
grid.draw(g)

enter image description here

关于r ggplot2 : grouping legend in bar plots,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45701279/

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