R:每个图的百分比标签独立的分面条形图 [英] R: Faceted bar chart with percentages labels independent for each plot
问题描述
在提供的图片中,百分比标签增加到49个%(第一方面)和51%(第二方面)。
我见过这个问题解决方法是聚合ggplot之外的数据。我宁愿不这样做,我相信这是一个更好的方法。
library(ggplot2)
library(scale)
set.seed(123)
df < - data.frame(x = rnorm(10000,mean = 100,sd = 50) )
df $ factor_variable< - cut(df $ x,right = TRUE,
breaks = c(0,25,50,100,200,10000),
标签= c(0-25,26-50,51-100,101-200,> 200)
)
df $ second_factor_variable < - ifelse(df $ x <100,1,2)
df< - sample(df,x> 0)
table(df $ second_factor_variable )
p1 < - ggplot(df,aes(x = factor_variable,y =(.. count /)/ sum(.. count ..),ymax = 0.8))
1 p1 < - p1 + geom_bar(fill =deepskyblue3,width = .5)
p1 < - p1 + stat_bin(geom =text,
aes(label = paste (..count ..)/ sum(.. count ..)* 100),%)),
vjust = -1,color =grey30,siz e = 6)
p1 < - p1 + xlab(NULL)+ ylab(NULL)
p1 < - p1 + scale_y_continuous(label = percent_format())
p1 < - p1 + xlim(0-25,26-50,51-100,101-200,> 200)
p1 < - p1 + facet_grid(。〜second_factor_variable)
print(p1)
这种方法暂时适用,但PANEL变量没有记录,根据哈德利不应该使用。
看起来正确它的方式来汇总数据,然后绘图,有很多这样的例子在SO。
ggplot(df,aes( x = factor_variable,y =(..count ..)/ sapply(PANEL,FUN = function(x)sum(count [PANEL == x])))+
geom_bar(fill =deepskyblue3,宽度= .5)+
stat_bin(geom =text,
aes(label = paste(round((.. count ..)/ sapply(PANEL,FUN = function(x)sum count [PANEL == x]))* 100),%)),
vjust = -1,color =grey30,size = 6)+
facet_grid(。〜second_facto r_variable)
I'm trying to use facet_grid to produce several plots where each plot's percentage labels add to 100%.
In the image provided, the percentages labels add to 49% (first facet) and 51% (second facet).
I've seen this Question where the solution is to aggregate the data outside ggplot. I'd rather not do that, I believe this is a better approach.
library("ggplot2")
library("scales")
set.seed(123)
df <- data.frame(x = rnorm(10000, mean = 100, sd = 50))
df$factor_variable <- cut(df$x, right = TRUE,
breaks = c(0, 25, 50, 100, 200, 10000),
labels = c("0 - 25", "26 - 50", "51 - 100", "101 - 200", "> 200")
)
df$second_factor_variable <- ifelse(df$x < 100, 1, 2)
df <- sample(df, x > 0)
table(df$second_factor_variable)
p1 <- ggplot(df, aes(x = factor_variable, y = (..count..)/sum(..count..), ymax = 0.8))
p1 <- p1 + geom_bar(fill = "deepskyblue3", width=.5)
p1 <- p1 + stat_bin(geom = "text",
aes(label = paste(round((..count..)/sum(..count..)*100), "%")),
vjust = -1, color = "grey30", size = 6)
p1 <- p1 + xlab(NULL) + ylab(NULL)
p1 <- p1 + scale_y_continuous(label = percent_format())
p1 <- p1 + xlim("0 - 25", "26 - 50", "51 - 100", "101 - 200", "> 200")
p1 <- p1 + facet_grid(. ~ second_factor_variable)
print(p1)
This method for the time being works. However the PANEL variable isn't documented and according to Hadley shouldn't be used. It seems the "correct" way it to aggregate the data and then plotting, there are many examples of this in SO.
ggplot(df, aes(x = factor_variable, y = (..count..)/ sapply(PANEL, FUN=function(x) sum(count[PANEL == x])))) +
geom_bar(fill = "deepskyblue3", width=.5) +
stat_bin(geom = "text",
aes(label = paste(round((..count..)/ sapply(PANEL, FUN=function(x) sum(count[PANEL == x])) * 100), "%")),
vjust = -1, color = "grey30", size = 6) +
facet_grid(. ~ second_factor_variable)
这篇关于R:每个图的百分比标签独立的分面条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!