-1

I drew up a barplot and it almost worked well. However, the description on the x axis does not go along with the pair of bars plotted above each tick along the x axis. So, the two bars plotted on the tick "Motivational Problems" actually belong to the tick "No Problems". Further, the two bars plotted on the tick "Knowledge related Problems" belong actually to the tick "Motivational Problems". Further, the two bars on the tick "Both Problems" actually belong to the tick No Problems.

Can someone help me correcting my code? I want the following order of bars: No problems, motivational problems, knowledge related problems, both problems.

Strategytype <- c("Cognitive", "Cognitive", "Cognitive", "Cognitive", 
                  "Motivational", "Motivational", "Motivational", "Motivational")
Problem <- c("No Problems", "Motivational Problems", "Knowledge related Problems", 
             "Both Problems", "No Problems", "Motivational Problems", 
             "Knowledge related Problems", "Both Problems")
len <- c(1.97, 0.61, 2.25, 1.19, 0.61, 0.51, 1.36, 1.41)
sd <- c(0.06, 0.03, 0.15, 0.04, 0.06, 0.25, 0.17, 0.25)
df <- cbind(Strategytype, Problem, len, sd)
df <- as.data.frame(df)

df$Problem <- levels(df$Problem) <- c("No Problems", "Motivational Problems", 
                                      "Knowledge related Problems", 
                                      "Both Problems", "No Problems", 
                                      "Motivational Problems", 
                                      "Knowledge related Problems")
df$len <- as.numeric(df$len)
df$sd <- as.numeric(df$sd)

len <- ("Anzahl Strategytypen (KI 95%)")

p <- ggplot(df, aes(x = Problem, y = len, fill = Strategytype)) + 
  geom_bar(stat = "identity", color="black", position=position_dodge()) +
  geom_errorbar(aes(ymin = len-sd, ymax = len+sd), 
                width=.2, position = position_dodge(.5)) 
print(p)

df$len <- c(1.97, 0.61, 2.25, 1.19, 0.61, 0.51, 1.36, 1.41)
df$sd <- c(0.06, 0.03, 0.15, 0.04, 0.06, 0.25, 0.17, 0.25)
df$len <- as.numeric(df$len)
df$sd <- as.numeric(df$sd)

p <- ggplot(df, aes(x=Problem, y=len, fill=Strategytype)) + 
  geom_bar(width = 0.5, stat = "identity", color = "black", 
           position = position_dodge()) +
  scale_fill_manual(values = c('darkgrey', 'firebrick'))+
  geom_errorbar(aes(ymin = len-sd, ymax = len+sd), 
                width = .2, position = position_dodge(.5)) 
print(p)

p + 
  scale_x_discrete(breaks = c("No Problems", "Motivational Problems", 
                              "Knowledge related Problems", "Both Problems"),
                   labels = c("No Problems", "Motivational Problems", 
                              "Knowledge related \n Problems", "Both Problems")) + 
  theme_classic()

last_plot() + ylab("Anzahl kognititver und motivationaler\n Strategytypeen (KI 95%)")

last_plot() + xlab("Problemart")

enter image description here

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Nadine M.
  • 95
  • 2
  • 10
  • 3
    Possible duplicate of [Order Bars in ggplot2 bar graph](https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph) - Have a look at the answers to this linked question. You should follow the advice on how to reorder the bars using factor levels. If you do that correctly, you won't need the `breaks` argument in `scale_x_discrete` – dww Feb 03 '19 at 18:47
  • Thank you but it still failed. I know exactly how to change the labels but I cannot chance the order of the bars... – Nadine M. Feb 03 '19 at 19:36

1 Answers1

2

I drew up a barplot and it almost worked well. However, the description on the x axis does not go along with the pair of bars plotted above each tick along the x axis. So, the two bars plotted on the tick "Motivational Problems" actually belong to the tick "No Problems". Further, the two bars plotted on the tick "Knowledge related Problems" belong actually to the tick "Motivational Problems". Further, the two bars on the tick "Both Problems" actually belong to the tick No Problems.

I may be wrong but I suspect the incorrect association of labels to bars that you see might be related to the version of R and ggplot you are using. With R 3.5.2 and ggplot2 3.1.0 that does not occur using your code.

Can someone help me correcting my code? I want the following order of bars: No problems, motivational problems, knowledge related problems, both problems.

As dww pointed out in his comment, there are existing posts that explain how to order the bars.

Nonetheless, the following shows two ways to get the bars ordered the way you want. The first reorders the factor for Problem and the second uses the limits parameter to scale_x_discrete. If you use the second method you'll obviously want to change the order to the order you requested.

From help(scale_x_discrete):

limits A character vector that defines possible values of the scale and their order.

#!/usr/bin/env Rscript                                                                                                                                                                                           

library(ggplot2)
library(stringr)
library(cowplot)

Strategytype <- c("Cognitive", "Cognitive", "Cognitive", "Cognitive",
                  "Motivational", "Motivational", "Motivational", "Motivational")

Problem <- c("No Problems", "Motivational Problems", "Knowledge related Problems",
             "Both Problems", "No Problems", "Motivational Problems",
             "Knowledge related Problems", "Both Problems")

len <- c(1.97, 0.61, 2.25, 1.19, 0.61, 0.51, 1.36, 1.41)
sd <- c(0.06, 0.03, 0.15, 0.04, 0.06, 0.25, 0.17, 0.25)
df <- data.frame(Strategytype, Problem, len, sd)

g <- ggdraw()

# reorder bars by explicitly ordering factor levels                                                                                                                                                              

x.ordered <- factor(Problem, levels=c("No Problems", "Motivational Problems",
                                      "Knowledge related Problems", "Both Problems"))

p <- ggplot(df, aes(x=x.ordered, y=len, fill=Strategytype)) +
    geom_bar(width = 0.5, stat = "identity", color = "black",
             position = position_dodge()) +
    scale_fill_manual(values = c('darkgrey', 'firebrick')) +
    geom_errorbar(aes(ymin = len-sd, ymax = len+sd),
                  width = 0.2, position = position_dodge(0.5)) +
    labs(x="Problemart", y="Anzahl Strategytypen (KI 95%)") +
    theme_classic()

p1 <- p + scale_x_discrete(labels = function(x) str_wrap(x, width = 10))

# reorder bars using scale_x_discrete limits. See help(scale_x_discrete)                                                                                                                                         

p2 <- p +
    scale_x_discrete(limits = c("Motivational Problems", "No Problems",
                                "Knowledge related Problems", "Both Problems"),
                     labels = function(x) str_wrap(x, width = 10))

# draw plots                                                                                                                                                                                                     

g <- g + draw_plot(p1, 0, 0, 1, 0.5)
g <- g + draw_plot(p2, 0, 0.5, 1, 0.5)

print(g)

enter image description here

Mike N.
  • 1,662
  • 1
  • 13
  • 19