R语言 为什么我的y轴总是显示错误的值?

hrirmatl  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(171)

我有这张table:

structure(list(Species = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("setosa", 
"versicolor", "virginica"), class = "factor"), Variables = c("PetalLength", 
"PetalLength", "PetalLength", "PetalLength", "PetalLength", "PetalLength", 
"PetalLength", "PetalLength", "PetalWidth", "PetalWidth", "PetalWidth", 
"PetalWidth", "PetalWidth", "PetalWidth", "PetalWidth", "PetalWidth", 
"SepalLength", "SepalLength", "SepalLength", "SepalLength", "SepalLength", 
"SepalLength", "SepalLength", "SepalLength", "SepalWidth", "SepalWidth", 
"SepalWidth", "SepalWidth", "SepalWidth", "SepalWidth", "SepalWidth", 
"SepalWidth"), Values = c(4.7, 4.5, 4.9, 4.7, 4.4, 4.8, 4.5, 
4.7, 1.4, 1.5, 1.5, 1.6, 1.4, 1.8, 1.6, 1.5, 7, 6.4, 6.9, 6.3, 
6.7, 5.9, 6, 6.7, 3.2, 3.2, 3.1, 3.3, 3.1, 3.2, 3.4, 3.1)), row.names = c(NA, 
-32L), class = c("tbl_df", "tbl", "data.frame"))

即使我试图绘制最基本的条形图,也就是说:

grafico <- ggplot (data = tabela, aes (x = Variables, y = Values)) +
geom_bar (stat = "identity") +
windows (); grafico

发生以下情况:

如果值是从0到10的小数,为什么y轴一直显示从0到60的值???

2eafrhcq

2eafrhcq1#

正如注解中提到的,您正在堆叠条形图-每个x轴值都有多个值。
所以,你可以做一些事情,但最终,为了显示“正确的值”,你必须在每个x轴组中只有一个值(除非你也用另一个变量进行颜色编码)。
您可以通过ID的颜色编码来查看您的个人点。
举例来说:

library(dplyr)

tabela %>% 
   group_by(Species, Variables) %>% 
   mutate(ID = 1:n()) %>%
   ggplot(aes (x = Variables, y = Values, fill= as.factor(ID))) +
   geom_bar (stat = "identity")

现在是闪避,而不是叠加

tabela %>% 
   group_by(Species, Variables) %>% 
   mutate(ID = 1:n()) %>%
   ggplot(aes (x = Variables, y = Values, fill= as.factor(ID))) +
   geom_bar (stat = "identity", position = "dodge")

相关问题