RalfB
RalfB

Reputation: 583

ggplot2 flipping factor labels

I am not getting to the ground of this. I have a faceted bar chart using ggplot2 and looking for a way to:

1) make it display categories that have not been selected by participants of my questionnaire (e.g. if category 5 ("Horrible") has never been selected, I still want it to be displayed in the diagram (without a bar) to show the full set of possible answers and make the diagram more easy to read).

2) I want to flip the Y-axis in the faceted diagram so that "Great" is on top and "Horrible" is on the bottom. Is there a way to do this without changing the data?

Enclosed I put a complete, self-contained mini-example of what I currently have. I am still a R-Newbee, so if you have suggestions of how to make this shorter and simpler (I am sure there is plenty to reduce), I am happy to hear your suggestions.

data.csv:

ONE;TWO;THREE;FOUR
1;1;2;1
1;2;3;2  
2;2;3;2
3;2;4;3

code.R

library(ggplot2)
library(scales)
df = read.csv2("data.csv", fileEncoding="UTF-8")

df[df=="0"]<-NA;
df[df=="1"]<-"Great";
df[df=="2"]<-"Good";
df[df=="3"]<-"OK";
df[df=="4"]<-"Not Good";
df[df=="5"]<-"Horrible";

# four entries
df$ID = c(1:4);
# turn to factors
df$ONE = factor(df$ONE, levels = c("NA", "Great", "Good", "OK", "Not Good", "Horrible"));
df$TWO = factor(df$TWO, levels = c("NA", "Great", "Good", "OK", "Not Good", "Horrible"));
df$THREE = factor(df$THREE, levels = c("NA", "Great", "Good", "OK", "Not Good", "Horrible"));
df$FOUR = factor(df$FOUR, levels = c("NA", "Great", "Good", "OK", "Not Good", "Horrible"));

df.molten <- melt(df, id.vars="ID");
df.molten$value <- factor( df.molten$value, levels = c("NA", "Great", "Good", "OK", "Not Good", "Horrible") );

ggplot( df.molten , aes( x = value, fill=value )  ) + 
  geom_bar(aes(y = (..count..)/8)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, face=2), 
        legend.position="none", 
        axis.text= element_text(size=12), 
        axis.title=element_text(size=14,face="bold"), 
        panel.background = element_rect(fill = "white")) +
  scale_fill_manual(values=c("blue4","steelblue2", "blue4","steelblue2", "blue4")) +
  facet_wrap( "variable" ) + 
  scale_y_continuous(breaks=seq(0, 1, 0.1), labels = percent_format()) + 
  xlab("") +  
  ylab("") + 
  coord_flip();

Upvotes: 1

Views: 345

Answers (1)

eipi10
eipi10

Reputation: 93861

Add scale_x_discrete(drop=FALSE) to keep empty factor levels. To reverse the order of the factor levels without changing the original data frame I make the change within the call to ggplot. I've made a few other tweaks to the code as well. Also, you don't need all those semi-colons.

library(ggplot2)
library(scales)
library(reshape2)  # For melt function
library(dplyr)     # For chaining (%>%) operator

df = read.csv2("data.csv", fileEncoding="UTF-8")

df[df=="0"]<-NA;      # Removed quotes from NA 
df[df=="1"]<-"Great";
df[df=="2"]<-"Good";
df[df=="3"]<-"OK";
df[df=="4"]<-"Not Good";
df[df=="5"]<-"Horrible";

# four entries
df$ID = c(1:4);

df.molten <- melt(df, id.vars="ID");

# Only need to set factor levels once. No need for the other four calls to factor in 
# the original code. Also, I removed the "NA" level.
df.molten$value <- factor(df.molten$value, levels = c("Great", "Good", "OK", "Not Good", "Horrible"));

# First line reverses factor levels
ggplot(df.molten %>% mutate(value=factor(value, levels=rev(levels(value)))), 
        aes(x = value, fill=value)) + 
  geom_bar(aes(y = (..count..)/8)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, face=2), 
        legend.position="none", 
        axis.text= element_text(size=12), 
        axis.title=element_text(size=14,face="bold"), 
        panel.background = element_rect(fill = "white")) +
  scale_fill_manual(values=c("blue4","steelblue2", "blue4","steelblue2", "blue4")) +
  facet_wrap( "variable" ) + 
  scale_y_continuous(breaks=seq(0, 1, 0.1), labels = percent_format()) + 
  scale_x_discrete(drop=FALSE) +   # To keep empty factor levels
  labs(x="", y="") +               # Just to be more concise
  coord_flip()

enter image description here

Upvotes: 1

Related Questions