2016-01-22 71 views
0

给定一个数据帧如下:绘图条以降序显示特定列的值的计数?

V1   V2 
a   089 
a   065 
a   012 
b   101 
b   110 

现在,我想绘制,酒吧,有在作为y轴的第一列V1的值的计数,它应该是降序排列。

我曾尝试:

library(ggplot2) 
ggplot(data = df, aes(reorder(V1,..count..), y = ..count..)) +geom_bar(stat = "count") 

但失败并生成一个警告:

Warning messages: 
1: In min(x, na.rm = na.rm) : 
no non-missing arguments to min; returning Inf 
2: In max(x, na.rm = na.rm) : 
no non-missing arguments to max; returning -Inf 
3: In min(diff(sort(x))) : no non-missing arguments to min; returning Inf 
4: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL' 
5: Computation failed in `stat_count()`: 
arguments imply differing number of rows: 0, 1 

我也试图改变stat = "bin",但它没有工作也没有。 你有什么想法吗?

在此先感谢!

+0

仅供参考,这是不是你想要绘制的直方图,这是一个柱状图。 – MLavoie

+0

@MLavoie谢谢你的提醒。我改变了标题。 – user5779223

+0

@MLavoie它是一个直方图 – mtoto

回答

0

如果你需要你的直方图降序排列,您需要更改的水平V1第一:

df$V1 <- factor(df$V1, levels = names(sort(table(df$V1), decreasing = TRUE))) 

然后我们可以使用

library(ggplot2) 

# with qplot 
qplot(df$V1, geom="histogram") 

# with ggplot 
ggplot(df, aes(V1)) + geom_histogram() 

enter image description here

+0

但似乎使用qplot不能让我自定义x轴? – user5779223

+0

我想更改字体大小并使其垂直于x轴 – user5779223

+0

@moto这是我最初使用的。但是x轴没有正确的顺序。 – user5779223

0

你的代码提示你试图绘制条形图而不是直方图。 Asuming你确实想找一个条形图尝试stat="identity(否则看一看geom_histogram

ggplot(data = df, aes(reorder(V1,V2), y = V2)) +geom_bar(stat = "identity")

这是你在找什么?

enter image description here

+0

使用'reorder(V1,-V2)'如果你想降序 –

+0

感谢您的回复。但是我的要求中的顺序是根据V1值的数量,但是V2。其实,你可以忽略V2。 – user5779223