我正在绘制一个分类变量,而不是显示每个类别值的计数.
我正在寻找一种方法来ggplot显示该类别中的值的百分比.当然,有可能用计算的百分比创建另一个变量并绘制一个变量,但我必须做几十次,我希望在一个命令中实现它.
我正在尝试类似的东西
qplot(mydataf) +
stat_bin(aes(n = nrow(mydataf), y = ..count../n)) +
scale_y_continuous(formatter = "percent")
Run Code Online (Sandbox Code Playgroud)
但我必须错误地使用它,因为我有错误.
为了轻松重现设置,这里有一个简化的例子:
mydata <- c ("aa", "bb", NULL, "bb", "cc", "aa", "aa", "aa", "ee", NULL, "cc");
mydataf <- factor(mydata);
qplot (mydataf); #this shows the count, I'm looking to see % displayed.
Run Code Online (Sandbox Code Playgroud)
在实际情况中,我可能会使用ggplot而不是qplot,但使用stat_bin的正确方法仍然无法使用.
我也试过这四种方法:
ggplot(mydataf, aes(y = (..count..)/sum(..count..))) +
scale_y_continuous(formatter = 'percent');
ggplot(mydataf, aes(y = (..count..)/sum(..count..))) +
scale_y_continuous(formatter = 'percent') + geom_bar();
ggplot(mydataf, aes(x = levels(mydataf), y = …Run Code Online (Sandbox Code Playgroud) dbv的数据示例:
gender Sektion
1 m 5
2 m 5
3 w 3B
4 w 3B
5 w 3B
6 m 4
Run Code Online (Sandbox Code Playgroud)
我有以下情节:
Sekplot <- ggplot(dbv,aes(x=Sektion,
fill=factor(gender),
stat="bin",
label = paste(round((..count..)/sum(..count..)*100), "%")))
Sekplot <- Sekplot + geom_bar(position="fill")
Sekplot <- Sekplot + scale_y_continuous(labels = percent)
Sekplot <- Sekplot + labs(title = "test")
Sekplot <- Sekplot + scale_fill_discrete(name="test", breaks=c("m", "w", "k.A."), labels=c("m", "w", "k.A."))
Sekplot <- Sekplot + geom_hline(aes(yintercept = ges, linetype = "test"), colour = "black", size = 0.75, show_guide = T) …Run Code Online (Sandbox Code Playgroud) 我正在努力在我用R中的ggplot2制作的图表中获得正确的变量排序.
假设我有一个数据帧,例如:
set.seed(1234)
my_df<- data.frame(matrix(0,8,4))
names(my_df) <- c("year", "variable", "value", "vartype")
my_df$year <- rep(2006:2007)
my_df$variable <- c(rep("VX",2),rep("VB",2),rep("VZ",2),rep("VD",2))
my_df$value <- runif(8, 5,10)
my_df$vartype<- c(rep("TA",4), rep("TB",4))
Run Code Online (Sandbox Code Playgroud)
产生下表:
year variable value vartype
1 2006 VX 5.568517 TA
2 2007 VX 8.111497 TA
3 2006 VB 8.046374 TA
4 2007 VB 8.116897 TA
5 2006 VZ 9.304577 TB
6 2007 VZ 8.201553 TB
7 2006 VD 5.047479 TB
8 2007 VD 6.162753 TB
Run Code Online (Sandbox Code Playgroud)
有四个变量(VX,VB,VZ和VD),属于两组变量类型(TA和TB).
我想将值绘制为y轴上的水平条,首先按变量组垂直排序,然后按变量名称排序,按年份分面,x轴上的值和填充颜色对应于变量组.(即在这个简化的例子中,顺序应该是,从上到下,VB,VX,VD,VZ)
1)我的第一次尝试是尝试以下方法:
ggplot(my_df,
aes(x=variable, y=value, fill=vartype, order=vartype)) + …Run Code Online (Sandbox Code Playgroud) 从这个问题我们看到一个简单geom_line的答案。
library(dplyr)
BactData %>% filter(year(Date) == 2017) %>%
ggplot(aes(Date, Svartediket_CB )) + geom_line()
Run Code Online (Sandbox Code Playgroud)
如果我们更改geom_line为,geom_bar我们可能会期望看到条形图,但是
错误:stat_count() 不得与任何美学一起使用。
但是如果我们添加它会起作用stat = "identity",就像这样
library(dplyr)
BactData %>% filter(year(Date) == 2017) %>%
ggplot(aes(Date, Svartediket_CB )) + geom_bar(stat = "identity")
Run Code Online (Sandbox Code Playgroud)
为什么没有geom_bar工作stat = "identity"- 即什么是目的stat = "identity"?
更新了问题以纳入已在SO上回答的部分解决方案
我正在使用ggplot2创建几个图并将图gridExtra组合成一个图,其中包含多个面板,所有图都在一列中.我的问题是我不能让点图行之间的空间在两个图中都是一致的.

library(ggplot2)
# data
dat1 <- data.frame(VARIABLES=c("Item 1", "Item 2 is a little longer"),
est=c(.3, .5),
min=c(.2, .4),
max=c(.4, .7))
dat2 <- data.frame(VARIABLES=c("Item 3",
"Item 4 is even longer if you can believe it",
"And there is a third item",
"And a fourth item"),
est=c(.3, .5, .3, .5),
min=c(.2, .4, .2, .4),
max=c(.4, .7, .4, .7))
dat <- c("dat1", "dat2")
labs <- c("Plot 1", "Plot2")
# create plots
count <- 1
for (i in dat) …Run Code Online (Sandbox Code Playgroud) 这是我经常遇到的问题,我只需要帮助整理这个问题.我正在尝试使用ggplot绘制已排序的数据框.但是,绘图不按照我的数据框中的顺序排序.
举例来说明我的问题:
value <- c(5,8,9,11,3)
Attribute <- c("a", "b", "c","d","e")
my.order <- as.factor(c(4,3,2,1,5))
my.df <- data.frame(Attribute,value,my.order)
my.df
# Attribute value my.order
#1 a 5 4
#2 b 8 3
#3 c 9 2
#4 d 11 1
#5 e 3 5
Run Code Online (Sandbox Code Playgroud)
然后我通过my.order命令数据框,属性列
my.df.ordered <- my.df[with(my.df, order(my.order, Attribute)), ]
my.df.ordered
# Attribute value my.order
#4 d 11 1
#3 c 9 2
#2 b 8 3
#1 a 5 4
#5 e 3 5
Run Code Online (Sandbox Code Playgroud)
这一切都很好,但是当我尝试用ggplot绘制它时,属性按字母顺序再次排序....
ggplot(my.df.ordered, aes(x=Attribute,y=value))+ geom_point()+ coord_flip()
Run Code Online (Sandbox Code Playgroud)
请帮忙?