Reputation: 35
I am trying to create a bar plot in R using ggplot, displaying the year a document was published as an x-variable and a mean_sentiment score of that document on the y-axis.
However, I am pretty new to R and there are two problems that I cannot figure out:
The x-axis reporting the year is labelled in 10-year steps. I however, want every bar labelled with the respective year.
There are some years for which there is no data. In my current version, these missing years are left blank. I want to erase that space (do I have to treat the year variable as a factor somehow?)
Thank you very much for any help, I'm a little lost here.
library(ggplot2)
sent_results$posneg_label <- ifelse(sent_results$mean_sentiment < 0, "Negative", "Positive")
ggplot(sent_results, aes(year, mean_sentiment)) + geom_bar(stat = "identity", aes(fill=posneg_label)) +
scale_fill_manual(name="Mean Sentiment towards China",
labels = c("Positive", "Negative"),
values = c("Positive"="green", "Negative"="red"))
Upvotes: 1
Views: 1104
Reputation: 283
If I'm understanding you correctly, for (1) you just need to do scale_x_continuous(breaks = seq(first_year, last_year, by = 1)
For (2), indeed treating year as a factor would be an efficient solution (and then you don't need to do the above code). If you wrap your year aes like this: factor(year)
you should get what you want.
Upvotes: 5