0

I created a stacked barplot that counts tickets by different state, unfortunately the related numbers are reverse of that how they should be and how they also are in the data frame

Here is my code for the plot:

library(ggplot2)
PlotTotal <- ggplot(data=DevBarPlot, aes(x=DayOfMonth, y=NumOfTickets, fill=State), break(31)) +
    geom_bar(stat="identity")+
    scale_fill_manual(values=c("lightsteelblue2", "lightsteelblue3"))+
    coord_cartesian(ylim=c(2000,30000))+
    geom_text(aes(y=NumOfTickets, label=NumOfTickets), vjust=-1, color="black", size=3.4)+
    labs(title="Num of Tickets per Day", x="Day of Month", y = "Number of Tickets")+
    theme_classic() + 
    theme(legend.position="top")+
    theme(legend.background = element_rect(fill="white",size=0.3, linetype="solid",colour ="lightsteelblue3"))+    
    theme(plot.title = element_text(color="lightsteelblue2", size=14, face="bold",hjust=0.5))
  • 1
    maybe th reverse `rev` function will come in handy – Brian Davis Feb 13 '18 at 18:22
  • thanks, where would you insert the rev? – Mow Osterberger Feb 13 '18 at 18:45
  • Sorry. Your code isn't reproducible and I'm not a ggplot guy. Although hacky, it does seem to reason that you could reverse appropriate columns in your data to achieve your desired output, e.g., `df[,1] <- rev(df[,1])` – Brian Davis Feb 13 '18 at 19:15
  • Hi @MowOsterberger. If my answer below solves your problem, I'd be grateful if you could upvote it and mark it as accepted. Thanks. – Greg Feb 18 '18 at 11:31

1 Answers1

0

It's helpful to provide some sample data (see this answer for help). In this case I've created a sample data frame from the data in your two images, guessing that the other state is NY.

data <- data.frame(
  State = c(rep('NOTNY', 5), rep('NY', 5)),
  NumOfTickets = c(5893, 5681, 4629, 2508, 5363,
                   23322, 20827, 15259, 6271, 18192),
  DayOfMonth = rep(1:5, 2)
)

data
#>    State NumOfTickets DayOfMonth
#> 1  NOTNY         5893          1
#> 2  NOTNY         5681          2
#> 3  NOTNY         4629          3
#> 4  NOTNY         2508          4
#> 5  NOTNY         5363          5
#> 6     NY        23322          1
#> 7     NY        20827          2
#> 8     NY        15259          3
#> 9     NY         6271          4
#> 10    NY        18192          5

In terms of the plot, you needed to include position = "stack" in your geom_text. I've also tweaked a few other bits and bobs e.g. vjust = 1.5 and using geom_col().

library(ggplot2)

ggplot(data, aes(x = DayOfMonth, y = NumOfTickets, fill = State)) +
  geom_col() +
  scale_fill_manual(values = c("lightsteelblue2", "lightsteelblue3")) +
  geom_text(aes(y = NumOfTickets, label = NumOfTickets),
            position = "stack", vjust = 1.5, size = 3.4) +
  labs(title = "Num of Tickets per Day",
       x = "Day of Month", y = "Number of Tickets") +
  theme_classic() +
  theme(legend.position = "top") +
  theme(legend.background = element_rect(
    fill = "white", size = 0.3, linetype = "solid", colour = "lightsteelblue3")) +
  theme(plot.title = element_text(
    color = "lightsteelblue2", size = 14, face = "bold", hjust = 0.5))

Greg
  • 487
  • 5
  • 15