以分面方式堆叠计算图
Stacking calculated plots in a facet manner
我的问题与 this question 类似,因为我想要相同的结果。但是我认为我的数据不能以相同的方式融化,因为它是根据 TRUE/FALSE 值计算的。
我的数据和图表如下:
library(ggplot2)
library(gridExtra)
library(grid)
library(scales)
library(RColorBrewer)
#test dataframe
player <- c("a", "b", "a", "b", "c",
"a", "a", "b", "c", "b",
"c", "a", "c", "c", "a",
"c", "c", "c", "c", "c",
"c", "c", "c", "c", "c",
"c", "c", "c", "c", "c",
"b", "b", "b", "b")
is.winner <- c(TRUE, TRUE, TRUE, TRUE, TRUE,
FALSE, TRUE, TRUE, TRUE, FALSE,
TRUE, TRUE, TRUE, TRUE, FALSE,
TRUE, FALSE, FALSE, FALSE, FALSE,
TRUE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE,
TRUE, TRUE, FALSE, FALSE)
df <- data.frame(player, is.winner)
df$is.winner <- factor(df$is.winner, levels=c("TRUE", "FALSE")) #swap T/F bars
# Stacked wins and losses
aa <- ggplot(data=df, aes(x=player, fill=is.winner)) +
stat_bin(geom = "bar", position = "stack") +
scale_fill_brewer(palette="Set2") +
coord_flip()
# Win percentage
ab <- ggplot(data=df, aes(x=player)) +
geom_bar(aes(fill=is.winner),position='fill')+
scale_y_continuous(labels=percent)+
scale_fill_brewer(palette="Set2") +
xlab("player") +
ylab("win%") +
coord_flip()
grid.arrange(aa,ab,ncol=2)
我们的目标是能够轻松查看玩家的获胜次数和获胜百分比,如上面不同的图表所示。我认为 facet-style 图表会很棒,但我不确定我处理数据的方式是否会影响我使用它的能力。感谢您的任何见解。
你可以制作一个虚拟分面变量,
d <- lattice::make.groups(first=df, second=df)
ggplot(data=d, aes(x=player, fill=is.winner)) +
facet_grid(which~., scales="free") +
geom_bar(position = "stack", data=subset(d, which=="first")) +
geom_bar(position = 'fill', data=subset(d, which=="second")) +
scale_fill_brewer(palette="Set2")
有趣的是,翻转坐标会产生不正确的绘图,至少对于 ggplot2 的开发版本是这样。
last_plot() + coord_flip()
我的问题与 this question 类似,因为我想要相同的结果。但是我认为我的数据不能以相同的方式融化,因为它是根据 TRUE/FALSE 值计算的。
我的数据和图表如下:
library(ggplot2)
library(gridExtra)
library(grid)
library(scales)
library(RColorBrewer)
#test dataframe
player <- c("a", "b", "a", "b", "c",
"a", "a", "b", "c", "b",
"c", "a", "c", "c", "a",
"c", "c", "c", "c", "c",
"c", "c", "c", "c", "c",
"c", "c", "c", "c", "c",
"b", "b", "b", "b")
is.winner <- c(TRUE, TRUE, TRUE, TRUE, TRUE,
FALSE, TRUE, TRUE, TRUE, FALSE,
TRUE, TRUE, TRUE, TRUE, FALSE,
TRUE, FALSE, FALSE, FALSE, FALSE,
TRUE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE,
TRUE, TRUE, FALSE, FALSE)
df <- data.frame(player, is.winner)
df$is.winner <- factor(df$is.winner, levels=c("TRUE", "FALSE")) #swap T/F bars
# Stacked wins and losses
aa <- ggplot(data=df, aes(x=player, fill=is.winner)) +
stat_bin(geom = "bar", position = "stack") +
scale_fill_brewer(palette="Set2") +
coord_flip()
# Win percentage
ab <- ggplot(data=df, aes(x=player)) +
geom_bar(aes(fill=is.winner),position='fill')+
scale_y_continuous(labels=percent)+
scale_fill_brewer(palette="Set2") +
xlab("player") +
ylab("win%") +
coord_flip()
grid.arrange(aa,ab,ncol=2)
我们的目标是能够轻松查看玩家的获胜次数和获胜百分比,如上面不同的图表所示。我认为 facet-style 图表会很棒,但我不确定我处理数据的方式是否会影响我使用它的能力。感谢您的任何见解。
你可以制作一个虚拟分面变量,
d <- lattice::make.groups(first=df, second=df)
ggplot(data=d, aes(x=player, fill=is.winner)) +
facet_grid(which~., scales="free") +
geom_bar(position = "stack", data=subset(d, which=="first")) +
geom_bar(position = 'fill', data=subset(d, which=="second")) +
scale_fill_brewer(palette="Set2")
有趣的是,翻转坐标会产生不正确的绘图,至少对于 ggplot2 的开发版本是这样。
last_plot() + coord_flip()