Ordering axis in torch plot using factor but including size in label

I have this facet plot and x-axis, I would like to do the ordering of the x-axis "outside fav", "ne", "mi", "fa", "outside un", but this is tricky because I also want to include counting observations in parentheses.

I made x a factor and put the dataframe in the correct order like this:

 dat$x = factor(dat$x, levels = c("outside fav","ne","mi", "fa","outside un") , ordered = TRUE)
      levels(dat$x)
      dat = dat %>% arrange(country, x) # order by country and x 

      

Then in the ggplot below, I do this:

factor( x_label, levels = unique(dat$x_label), ordered = TRUE)

      

to try and keep the labels in order, but the unique of course now includes the counter in parentheses, so the plot output is wrong. You can see this by looking at the "J" plot, where the order on the x-axis is fa (5), outside fav (5), and so on.

Do you know how to get the order on the x-axis in the correct WHILE order including count in parenthese?

Thank.

  dat =  data.frame(country = c(
                                    rep("A",4),
                                    rep("H", 5),
                                    rep("I",4),
                                    rep("J", 5),
                                    rep("S", 2),
                                    rep("T",4)    ),
                       x = c(
                                     "outside fav","mi", "fa","outside un",
                                     "outside fav","ne","mi", "fa","outside un",
                                     "outside fav","mi", "fa","outside un",
                                     "outside fav","ne","mi", "fa","outside un",
                                      "fa","outside fa",
                                      "ne","mi", "fa","outside un"  ), 
                 x_label = c(
                                     "outside fav (1)","mi(3)", "fa (5)","outside un (3)",
                                     "outside fav (6)","ne (8)","mi (57)", "fa (22)","outside un (6)",
                                     "outside fav (27)","mi (2)", "fa (4)","outside un (41)",
                                     "outside fav (5)","ne (5)","mi (8)", "fa (5)","outside un (8)",
                                      "fa (3)","outside fa (2)",
                                      "ne (2)","mi (2)", "fa (2)","outside un (1)"  ),
                 y = rnorm(24)
                 )
      dat$x = factor(dat$x, levels = c("outside fav","ne","mi", "fa","outside un") , ordered = TRUE)
      levels(dat$x)
      dat = dat %>% arrange(country, x) # order by country and x 
      ggplot(dat, aes(x = factor( x_label, levels = unique(dat$x_label), ordered = TRUE),
                      y = y   ))+ 
        geom_point()  +
        facet_wrap(~country, scales="free_x")+theme(axis.text.x = element_text(angle = 90))

      

+3


source to share


2 answers


I think you can get a graph by unique x-value (id) and label.

dat$id <- as.factor(1:24)
ggplot(dat, aes(x = id,
                y = y)) + 
  geom_point()  +
  facet_wrap(~ country, scales = "free_x") +
  scale_x_discrete(breaks = dat$id, labels = dat$x_label) + 
  theme(axis.text.x = element_text(angle = 90))

      



enter image description here

+1


source


The problem arises because your x_label coefficient is not ordered the way you want it to appear on the chart (see (dat $ x_label) levels). If you want to avoid sorting the factor levels manually, you can use this approach:

library(ggplot2)
library(gridExtra)  

p = list()
countries <- unique(dat$country)
for(i in 1:length(countries)) {
  dat2 = dat[dat$country == countries[i],]
  p[[i]] <- ggplot(dat2, aes(x = x, y = y))+ 
    geom_point()  + scale_x_discrete(breaks = dat2$x, labels = dat2$x_label) +
    ggtitle(countries[i])+ labs(x ="") +
    theme(axis.text.x = element_text(angle = 90))
}
x11(); grid.arrange(grobs = p, nrow = 2)

      



enter image description here

0


source







All Articles