R 在刻面网格中打印百分比标签
如果我有一些通用数据R 在刻面网格中打印百分比标签,r,ggplot2,facet,R,Ggplot2,Facet,如果我有一些通用数据 dx <- data.frame(x = c(sample(letters[1:4], 1000, replace=TRUE)), y = c(sample(letters[5:7], 1000, replace=TRUE)), z = c(sample(1:10, 5000, replace=TRUE))) dx$z <- as.factor(dx$z) d <- ggplot(dx
dx <- data.frame(x = c(sample(letters[1:4], 1000, replace=TRUE)),
y = c(sample(letters[5:7], 1000, replace=TRUE)),
z = c(sample(1:10, 5000, replace=TRUE)))
dx$z <- as.factor(dx$z)
d <- ggplot(dx, aes(z, fill=z)) +
geom_bar() +
facet_grid(y~x)
d
然后试图绘制
d <- ggplot(dx,
aes(z, fill =z)) +
geom_bar() +
facet_grid(y ~ x)+
geom_text(position = "identity", aes(label = paste(round(m$pct), "%")),
color = "black", y = 40) +
scale_x_discrete(drop=F)
非常感谢您提供的任何帮助,无论是
geom_text
中的命令还是使用ddply
都不太清楚您所指的z份额是多少,但这一项在整个网格中添加了百分比,最多为1
require(plyr)
helper<-sum(dx$z)
datac <- ddply(dx, c("x","y"), summarise, zshare=sum(z)/helper)
dx$z <- as.factor(dx$z)
d <- ggplot(data=dx)
d <- d + geom_bar(aes(z, fill=z))
d <- d+ facet_grid(y~x) + geom_text(data=datac,aes(x=3,y=60,label=paste(round(zshare,3)*100,"%")))
d
require(plyr)
helper不太清楚你所说的z的份额是多少,但这个例子将整个网格的百分比增加到1
require(plyr)
helper<-sum(dx$z)
datac <- ddply(dx, c("x","y"), summarise, zshare=sum(z)/helper)
dx$z <- as.factor(dx$z)
d <- ggplot(data=dx)
d <- d + geom_bar(aes(z, fill=z))
d <- d+ facet_grid(y~x) + geom_text(data=datac,aes(x=3,y=60,label=paste(round(zshare,3)*100,"%")))
d
require(plyr)
帮助者从您链接的问题中引出:
您还需要将ddply
返回的新数据帧与美学一起传递给geom_text
调用
library(ggplot2)
library(plyr)
# Reduced the dataset
set.seed(1)
dx <- data.frame(x = sample(letters[1:2], 1000, replace=TRUE),
y = sample(letters[5:6], 1000, replace=TRUE),
z = factor(sample(1:3, 5000, replace=TRUE)))
# Your ddply call
m <- ddply(data.frame(table(dx)), .(x,y), mutate,
pct = round(Freq/sum(Freq) * 100, 0))
# Plot - with a little extra y-axis space for the label
d <- ggplot(dx, aes(z, fill=z)) +
geom_bar() +
scale_y_continuous(limits=c(0, 1.1*max(m$Freq))) +
facet_grid(y~x)
d + geom_text(data=m, aes(x=z, y=Inf, label = paste0(pct, "%")),
vjust = 1.5, size = 5)
库(ggplot2)
图书馆(plyr)
#减少了数据集
种子(1)
dx从您链接的问题中引出:
您还需要将ddply
返回的新数据帧与美学一起传递给geom_text
调用
library(ggplot2)
library(plyr)
# Reduced the dataset
set.seed(1)
dx <- data.frame(x = sample(letters[1:2], 1000, replace=TRUE),
y = sample(letters[5:6], 1000, replace=TRUE),
z = factor(sample(1:3, 5000, replace=TRUE)))
# Your ddply call
m <- ddply(data.frame(table(dx)), .(x,y), mutate,
pct = round(Freq/sum(Freq) * 100, 0))
# Plot - with a little extra y-axis space for the label
d <- ggplot(dx, aes(z, fill=z)) +
geom_bar() +
scale_y_continuous(limits=c(0, 1.1*max(m$Freq))) +
facet_grid(y~x)
d + geom_text(data=m, aes(x=z, y=Inf, label = paste0(pct, "%")),
vjust = 1.5, size = 5)
库(ggplot2)
图书馆(plyr)
#减少了数据集
种子(1)
dx如果您可以使用dplyr
,您可以通过以下方式获得分组的z
百分比:dx%>%groupby(x,y)%%>%summary(pct=n()/sum(z))
如果您可以使用dplyr
,您可以通过以下方式获得分组的z
百分比:dx%>%groupby(x,y)%%>%summary(pct=n()/sum(z))
谢谢,但是我想要每个网格中每个z值(1:10)的百分比。(ae、be、ce、de、af…)即在每个网格内,百分比之和应为100,但我想要每个网格中每个z值(1:10)的百分比。(ae、be、ce、de、af…)即每个网格内的百分比总和应为100