0

I have been trying for a long time to get my graph to look like the colored one. Here is my code and what I have so far. I keep on getting the error:

"Warning message: position_stack requires non-overlapping x intervals".

enter image description here

enter image description here enter image description here

user438383
  • 5,716
  • 8
  • 28
  • 43
  • Perhaps you need a sequence like `2000+seq(0.125, 3, by= 0.25)` . Your "continuous" version of quarterly values has gaps in it. '2000.4' should probably be either `2000.75` or `2000.875` depending on how your data was developed. The zoo-pkg has a yrmon class. FURTHERMORE DO NOT POST IMAGES OF CODE. – IRTFM Mar 19 '22 at 23:52
  • 1
    Welcome to SO! Please see [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Please do not post an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557/15293191). You can provide your data via the output of `dput(df)` or to give a minimal subset use `dput(head(df))`. – AndrewGB Mar 19 '22 at 23:58
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 23 '22 at 10:42

1 Answers1

0

One way to correct your plot is to define the variable on the y-axis as a factor.

Sample code:

library(ggplot2)
library(ggthemes)

Q1$Quarter=factor(Q1$Quarter, levels=c("2020.1", "2020.2", "2020.3", "2020.4", "2021.1", "2021.2", "2021.3", "2021.4", "2022.1"))

ggplot(Q1, aes(x=Quarter, y=avgapprove))+
  geom_bar(position="stack", stat="identity")+
  geom_col(width=1, aes(fill=avgapprove))+
  labs(x="Year(By Quarter)", y="Approval Rating", fill="Approval", title="Public Opinion of Presidential Handling of COVID-19")+
  geom_text(aes(label=avgapprove),position = position_stack(),hjust=0.5, vjust=1, size = 5, color="white")+
  theme_pander()+
  theme(axis.text.x = element_text(hjust = 1, face="bold", size=12, color="black"), 
        axis.title.x = element_text( face="bold", size=16, color="black"),
        axis.text.y = element_text( face="bold", size=12, color="black"),
        axis.title.y = element_text( face="bold", size=16, color="black"),
        strip.text = element_text(size=10, face="bold"),
        plot.title = element_text(size=18, face="bold"),
        legend.title = element_text(size=16, face="bold"),
        legend.text = element_text(color = "black", size = 16,face="bold"),
        legend.position="right")+
  scale_x_discrete(breaks=c("2020.1", "2020.2", "2020.3", "2020.4", "2021.1", "2021.2", "2021.3", "2021.4", "2022.1"))+
  scale_y_continuous(breaks=seq(0,100, by=25))

Plot:

enter image description here

You can change the theme() to theme_classic()

enter image description here

Sample data:

Q1<-structure(list(Quarter = structure(1:9, .Label = c("2020.1", 
"2020.2", "2020.3", "2020.4", "2021.1", "2021.2", "2021.3", "2021.4", 
"2022.1"), class = "factor"), avgapprove = c(47, 43, 39, 40, 
56, 61, 54, 48, 44)), spec = structure(list(cols = list(Quarter = structure(list(), class = c("collector_double", 
"collector")), avgapprove = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), row.names = c(NA, 
-9L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))
Rfanatic
  • 2,224
  • 1
  • 5
  • 21