2016-01-22 70 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