0

I have a dataset:

data <- c('real','real','real','real','real','pred','pred','pred','pred','pred','real','real','real','real','pred','pred','pred','pred')
threshold <- c('>=1','>=2','>=3','>=4','>=101','>=1','>=2','>=3','>=4','>=101','>=1','>=2','>=3','>=4','>=1','>=2','>=3','>=4')
accuracy <- c(63.4,64.4,65.1,64.3,65.4,62.1,63.6,64.1,65.4,64.8,62.2,63.3,64.4,65.6,63.1,63.8,64.6,65.1)
types<-c('morning','morning','morning','morning','morning','morning','morning','morning','morning','morning','evening','evening','evening','evening','evening','evening','evening','evening')

df <- data.frame(data,threshold,accuracy,types)

I want to plot 'data' column as stacked barplot for morning and evening separately. So I use facet wrap. My code for plotting is:


ggplot(df, aes(x = threshold, y = accuracy)) + geom_bar(aes(fill = data), stat = "identity", color = "white",position = position_dodge(0.9))+
  facet_wrap(~types) + 
  fill_palette("jco")

And the plot I get looks like:

plot

However, as you can see the order of threshold got messed up. I want the order for morning to look like:

'>=1','>=2','>=3','>=4','>=101'

And the order for evening should be:

'>=1','>=2','>=3','>=4'

So I have three questions:

  1. How can I enforce the order using my code?

2 Also for evening I shouldn't be getting '>=101' so how can I remove that from the plot.

  1. Is there a way to make the background white but keep the grid.

  2. And on a slightly unrelated note, can you point at a graph type that might be slightly better looking than this? I am new at visualisation so I am still learning.

Insights will be appreciated.

M--
  • 25,431
  • 8
  • 61
  • 93
John
  • 815
  • 11
  • 31

1 Answers1

1

You may set order of threshold to reorder x axis.

Then add scales = 'free' in facet_wrap to remove >=101 in evening,

add theme_bw() to make background white.

df %>%
  mutate(threshold = factor(threshold, levels = c('>=1','>=2','>=3','>=4','>=101'))) %>%
  ggplot(aes(x = threshold, y = accuracy)) + geom_bar(aes(fill = data), stat = "identity", color = "white",position = position_dodge(0.9))+
  facet_wrap(~types, scales = 'free') +
  theme_bw() + 
  fill_palette("jco")

enter image description here

Park
  • 14,771
  • 6
  • 10
  • 29