Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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:ggplot2条形图显示Y轴刻度顺序错误_R_Ggplot2 - Fatal编程技术网

r:ggplot2条形图显示Y轴刻度顺序错误

r:ggplot2条形图显示Y轴刻度顺序错误,r,ggplot2,R,Ggplot2,我正在绘制一个多组条形图,发现尽管数据显示正确,但图表在显示时完全倾斜,因为y轴刻度值不是按数字顺序排列的 当前图形输出: 下面是我的代码,有人能告诉我如何对值进行数字排序吗 library(ggplot2) veg_db <- read.csv("filepath\\filename.csv", header = TRUE) veg_df <- data.frame(veg_db) veg <- veg_df[c(125),c(4,5,9,12,13,18)] w

我正在绘制一个多组条形图,发现尽管数据显示正确,但图表在显示时完全倾斜,因为y轴刻度值不是按数字顺序排列的

当前图形输出:

下面是我的代码,有人能告诉我如何对值进行数字排序吗

     library(ggplot2)
veg_db <- read.csv("filepath\\filename.csv", header = TRUE)
veg_df <- data.frame(veg_db)

veg <- veg_df[c(125),c(4,5,9,12,13,18)]
white_veg <- data.frame(t(veg))
colnames(white_veg) <- c("value")
rownames(white_veg) <- c("Broccoli", "Carrots", "Leafy Greens", "Peas", "Peppers", "Tomatoes")
white_veg <- cbind(vegetables = rownames(white_veg),ethnicity = "white", white_veg)


veg <- veg_df[c(129),c(4,5,9,12,13,18)]
black_veg <- data.frame(t(veg))
colnames(black_veg) <- c("value")
rownames(black_veg) <- c("Broccoli", "Carrots", "Leafy Greens", "Peas", "Peppers", "Tomatoes")
black_veg <- cbind(vegetables = rownames(black_veg),ethnicity = "black", black_veg)

total_veg <- merge(white_veg, black_veg, all = TRUE)
total_veg

plot <- ggplot(total_veg, aes(vegetables, value, fill = ethnicity))+
  geom_bar(position="dodge",stat="identity")


plot

值是通过str函数发现的一个因子,而不是数字


我使用as.numericas.character将值从因子转换为数值,然后在打印时以有意义的顺序显示数据。感谢@MrFlick为我指明了正确的方向。

请参阅。首选数据的dput,以便我们可以查看data.types。在data.frame中,值可能是一个因子/字符,而不是一个数值。这可能是由于cbind创建了一个矩阵,而不是data.frame。使用cbind而不是data.frame有什么原因吗?或者至少使用cbind.data.frame。那么它可能发生在转置t期间。这是一个针对矩阵的操作,而不是数据帧。没有原始的输入文件,很难重现和诊断确切的原因。我将cbind函数替换为data.frame函数,但它根本没有调整绘图。我运行了strtotal_veg,发现价值确实是一个因素。要转换它,我使用.numerica。character@MrFlick感谢所有的帮助,对r和stackoverflow来说还是很新的,您的反馈非常感谢strtotal_veg的回报是什么?值是一个因子而不是数字吗?
     vegetables ethnicity value
1      Broccoli     white 10.18
2      Broccoli     black  6.46
3       Carrots     white 12.58
4       Carrots     black  8.54
5  Leafy Greens     white  2.88
6  Leafy Greens     black  1.68
7          Peas     white 19.96
8          Peas     black 13.13
9       Peppers     white  3.09
10      Peppers     black  3.12
11     Tomatoes     white 80.13
12     Tomatoes     black 62.08