[英]How to set x,y in ggplot aes with one dimension dataframe?
df <- data.frame(
cola = c('1',NA,'c','1','1','e','1',NA,'c','d'),
colb = c("A",NA,"C","D",'a','b','c','d','c','d'),
colc = c('a',NA,'c','d','a',NA,'c',NA,'c','d'),stringsAsFactors = TRUE)
bad<-lapply(df, function(x) sum(is.na(x))/nrow(df))
bad<-as.data.frame(bad)
I want to make bar plot to one dimension dataframe bad
. 我想使对一维数据框的条形图
bad
。
X axis should be cola
, colb
, colc
,Y axis should be 0.2
, 0.1
, 0.3
. X轴应该是
cola
, colb
, colc
,Y轴应该是0.2
, 0.1
, 0.3
。 Then I tried but failed: 然后我尝试但失败了:
ggplot(bad,aes(x=colnames(bad), y=bad[1,])) +
geom_bar(stat='identity')
As to one dimension dataframe,how to set aes(x=?,y=?)
? 对于一维数据帧,如何设置
aes(x=?,y=?)
?
You need to convert your data frame to long format. 您需要将数据帧转换为长格式。 You can use reshape2's melt function.
您可以使用reshape2的melt函数。
dab <- reshape2::melt(bad)
> dab
variable value
1 cola 0.2
2 colb 0.1
3 colc 0.3
ggplot(dab) + geom_bar(aes(x=variable, y=value, fill=variable), stat='identity')
You need to change the dimension of the dataframe 您需要更改数据框的尺寸
library(ggplot2)
ggplot(stack(bad), aes(ind, values)) + geom_bar(stat='identity')
Or if you want to go the tidyverse
way we can use gather
as well 或者,如果你想要去的
tidyverse
方式,我们可以使用gather
以及
ggplot(tidyr::gather(bad), aes(key, value)) + geom_bar(stat='identity')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.