plot - R - How to display values of a ordered factor on axis with ggplot2 -
i display values of ordered factor on barplot ggplot2
(ideally, otherwise standard plot).
i have ordered factor one:
"how satisfied exercises :" - absolutely not satisfied => value 1 - not satisfied => value 2 - satisfied => value 3 - satisfied => value 4
i want plot barplot mean , values "absolutely not satisfied" -> "very satisfied" on axis instead 1 -> 4.
is possible ggplot2
? in point of view, main difficulty plot mean of factor , not distribution of values (actually plot made transformation of ordered factor integer).
here result of dput on dataset.
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"))
and here example of barplot (without values on axis...):
the code following.
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)
i can give further details if request not clear.
thanks
there's option called labels
in scale_y_continuous
can use this.
for example assume factor want use toto
. since set limits of 0 - 4 (i.e. length = 5) there 4 levels of toto
, added na
value of 0. set limits 0 - 3 or 1 - 4 without needing add na
level.
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)
for more details , examples, this great resource.
Comments
Post a Comment