Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 按降序显示某列值计数的绘图栏?_R_Plot_Ggplot2 - Fatal编程技术网

R 按降序显示某列值计数的绘图栏?

R 按降序显示某列值计数的绘图栏?,r,plot,ggplot2,R,Plot,Ggplot2,给出如下数据帧: V1 V2 a 089 a 065 a 012 b 101 b 110 现在我想画一个条形图,第一列V1中的值的计数作为y轴,它应该是降序的 我试过: library(ggplot2) ggplot(data = df, aes(reorder(V1,..count..), y = ..count..) ) +geom_bar(stat = "co

给出如下数据帧:

V1           V2
a            089
a            065
a            012
b            101
b            110
现在我想画一个条形图,第一列
V1
中的值的计数作为y轴,它应该是降序的

我试过:

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”
,但也没有成功。 你知道吗


提前谢谢

如果需要按降序排列直方图,则需要先更改
V1
的级别:

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

您的代码表明您正在尝试绘制条形图,而不是直方图。如果您确实在寻找条形图,请尝试
stat=“identity
(否则请查看
geom\u直方图

ggplot(数据=df,aes(重新排序(V1,V2,y=V2))+geom_条(stat=“identity”)

这就是你要找的吗


仅供参考,这不是您试图绘制的柱状图,而是条形图。@MLavoie谢谢提醒。我已经更改了标题。@MLavoie这是一个柱状图histogram@mtoto你是说geom_bar()是柱状图吗?他甚至更改了标题!他试图使用
geom_bar
生成柱状图,这是不正确的。但似乎使用qplot无法让我自定义x轴?我想更改字体大小,使其垂直于x轴-axis@moto这是我一开始使用的。但是x轴的顺序不正确。使用
 重新排序(V1,-V2)
如果您想要降序,谢谢您的回复。但我的要求中的顺序是根据V1值的数量,而不是V2。实际上,您可以忽略V2。
library(ggplot2)

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

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