Kumpelka - 1 year ago 121
R Question

# How to display values of a ordered factor on axis with ggplot2

I would like to display the values of an ordered factor on a barplot with

`ggplot2`
(ideally, otherwise with standard plot).

I have an ordered factor like this one:

"How are you satisfied with the exercises :"
- absolutely not satisfied => value 1
- not satisfied => value 2
- satisfied => value 3
- very satisfied => value 4

I want to plot a barplot with the mean and the values "absolutely not satisfied" -> "very satisfied" on the axis instead 1 -> 4.

Is it possible to do that with
`ggplot2`
? In my point of view, the main difficulty is to plot the mean of the factor and not a distribution of values (actually my plot is made with a transformation of the ordered factor to an integer).

Here is the result of dput on my 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 is the example of barplot (without the values on the axis...):

The code is the 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 you further details if my request is not clear.

Thanks

There's an option called `labels` in `scale_y_continuous` you can use for this.

For this example I assume that the factor you want to use is `Toto`. Since you set the limits of 0 - 4 (i.e. length = 5) but there are only 4 levels of `Toto`, I added `NA` for the value of 0. You also could set the limits as 0 - 3 or 1 - 4 without needing to add the `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 and examples, this is a great resource.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download