My R plot is displaying its axes completely incorrectly, can someone tell me why?

Viewed 29

I have used the following dataframe:

dataframe1 = data.frame(x1 = numeric(), x2 = character(), x3 = character(),
                        stringsAsFactors = FALSE)
dataframe1
dataframe1[1,] = c(8.10136702697, "Uncleared", "Before")
dataframe1[3,] = list(0.24816604371, "Uncleared", "After")
dataframe1[2,] = list(4.47101137777, "Cleared", "Before")
dataframe1[4,] = list(12.32421000000, "Cleared", "After")

and used the following code to plot:

plot =  ggplot(dataframe1) +
  geom_bar(aes(x = x3, y = x1, fill = factor(x2)), 
           stat="identity", position = "stack", width = 0.7) +
  coord_cartesian(ylim = c(0, 10)) +
  scale_fill_manual('',  values = c("red", "blue"), labels = c("Uncleared", "Cleared")) +
  labs(x="CCP",y="Nominal") +
  theme_bw(base_size = 14)

However, for some reason my axes are completely messed up. They are misaligned and more importantly, the two blocks stacked together should be the same length on a normal axis but instead I have something completely different. Can someone understand where its going wrong, I cannot figure it out

1 Answers

Your x1 is character. Transform it to numeric with as.numeric:

ggplot(dataframe1) +
  geom_bar(aes(x = x3, y = as.numeric(x1), fill = factor(x2)), 
           stat="identity", position = "stack", width = 0.7) +
  coord_cartesian(ylim = c(0, 10)) +
  scale_fill_manual('',  values = c("red", "blue"), labels = c("Uncleared", "Cleared")) +
  labs(x="CCP",y="Nominal") +
  theme_bw(base_size = 14)

enter image description here

Related