我想在条形图上显示排序因子的值ggplot2
(理想情况下,在标准图上)。
我有一个像这样的有序因素:
“您对练习的满意程度:”-绝对不满意=>值1-不满意=>值2-满意=>值3-非常满意=>值4
我想绘制一个均值而不是1-> 4的均值和值“绝对不满意”->“非常满意”的条形图。
有可能做到这一点ggplot2
吗?在我看来,主要困难在于绘制因子的均值而不是值的分布(实际上,我的绘制是通过将有序因子转换为整数来完成的)。
这是数据集上dput的结果。
structure(c(3L, 2L, 3L, 2L, 2L, 3L, 2L, NA, 2L, 3L, 4L, 2L, 1L
), .Label = c("pas du tout satisfait", "plutôt pas satisfait",
"plutôt satisfait", "très satisfait"), class = c("ordered",
"factor"))
这是barplot的示例(轴上没有值...):
代码如下。
Toto <- structure(c(3L, 2L, 3L, 2L, 2L, 3L, 2L, NA, 2L, 3L, 4L, 2L, 1L
), .Label = c("pas du tout satisfait", "plutôt pas satisfait",
"plutôt satisfait", "très satisfait"), class = c("ordered","factor"))
TotoNumeric <- as.data.frame(as.integer(Toto))
DataForGggplot2 <- as.data.frame(round(sapply(X = TotoNumeric, FUN = "mean", na.rm = TRUE), 1))
colnames(DataForGggplot2) <- "Donnees"
DataForGggplot2$Etiquette <- "the exercises"
Graphe <- ggplot(data = DataForGggplot2, aes(x = Etiquette, y = Donnees)) +
geom_bar(stat = "identity", fill = "blue") +
scale_y_continuous(limits = c(0, 4))
coord_flip()
print(Graphe)
如果我的要求不清楚,我可以为您提供更多详细信息。
谢谢
有一个称为一个选项labels
中scale_y_continuous
,你可以使用这个。
对于此示例,我假设您要使用的因子为Toto
。由于您将限制设置为0-4(即长度= 5),但只有4个级别Toto
,因此我添加NA
了0值。您也可以将限制设置为0-3或1-4,而无需添加NA
水平。
library(ggplot2)
Toto <- structure(c(3L, 2L, 3L, 2L, 2L, 3L, 2L, NA, 2L, 3L, 4L, 2L, 1L
), .Label = c("pas du tout satisfait", "plutôt pas satisfait",
"plutôt satisfait", "très satisfait"), class = c("ordered","factor"))
TotoNumeric <- as.data.frame(as.integer(Toto))
DataForGggplot2 <- as.data.frame(round(sapply(X = TotoNumeric, FUN = "mean", na.rm = TRUE), 1))
colnames(DataForGggplot2) <- "Donnees"
DataForGggplot2$Etiquette <- "the exercises"
Graphe <- ggplot(data = DataForGggplot2, aes(x = Etiquette, y = Donnees)) +
geom_bar(stat = "identity", fill = "blue") +
scale_y_continuous(name="Toto", labels = c("NA", levels(Toto)),limits = c(0, 4))
coord_flip()
print(Graphe)
有关更多详细信息和示例,这是一个很好的资源。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句