[R] Help needed in double bar plot made using ggplot2
bharat rawlley
bh@r@t_m_@|| @end|ng |rom y@hoo@co@|n
Fri Aug 20 00:02:08 CEST 2021
Thank you, Dr. Burradas!
That resolved my query
Have a great rest of your day
On Thursday, 19 August, 2021, 04:47:42 pm GMT-4, Rui Barradas <ruipbarradas using sapo.pt> wrote:
Hello,
Glad it helped.
As for making everything red, that only happens with the 2nd geom_text I
posted. And this is because color = "red" is not in aes().
In the 1st geom_text, I have aes( etc , color = gender)
and this makes the color depend on gender.
To make the text and bars colors the same, put color = gender in the
initial call to ggplot. Like this, all geom_ (layers) will inherit that
aesthetic.
And you don't need group = gender, fill or color already group the data.
ggplot(aes(x=year, y=percentage, fill=gender, color=gender), data =
graph_text)+
rest_of_code
Hope this helps,
Rui Barradas
Às 21:35 de 19/08/21, bharat rawlley escreveu:
> Thank you very much for the elaborate response, Dr. Barradas! It was
> extremely helpful!
>
> This resolves all my queries except one; I am unable to assign aesthetic
> colors in a way that the bar and text colors remain the same. I am not
> sure how to exactly assign color outside of aes. I used the following
> code which made everything red (and I want only text on top of the red
> bars to be red, rest to be blue according to their bar colors)
>
> ggplot(aes(x=year, y=percentage, group=gender, fill=gender), data =
> graph_text)+
> geom_bar(position = 'dodge', stat='identity')+
> theme_classic()+
> geom_text(aes(label = percentage), size = 5, position =
> position_dodge(width = 0.9), vjust=0, color = "Red") +
> scale_y_continuous(limits=c(0, 1.1*ymax))
>
> Thank you very much for your time and help, Dr. Barradas!
>
>
> On Thursday, 19 August, 2021, 02:07:44 pm GMT-4, Rui Barradas
> <ruipbarradas using sapo.pt> wrote:
>
>
> Hello,
>
> First, sample data.
>
> set.seed(2021)
> year <- rep(2016:2019, 2)
> percentage <- runif(length(year), 0.25, 0.70)
> gender <- rep(c("M", "F"), each = 4)
> graph_text <- data.frame(year, percentage, gender)
>
>
> 1) You have expand = c(0,0). Like this there is no space above the
> greatest bar. In order to make room, change to limits = c(0, ymax + 10
> percent).
>
> 2) Make the width of the bars larger, position_dodge(width = 0.9)
>
> 3) Assign a aesthetic color in geom_text, if you want the bars and text
> colors to be the same, assign a value outside aes() if you want only one
> color for the text. See at the end.
>
> 4) Not asked but I use scales::percent to make the percentages
> automatic. Change back to your code if not needed.
>
>
>
> ymax <- max(graph_text$percentage)
>
> ggplot(aes(x=year, y=percentage, group=gender, fill=gender), data =
> graph_text)+
> geom_bar(position = 'dodge', stat='identity')+
> geom_text(
> aes(label = scales::percent(percentage, accuracy = 0.1), color =
> gender),
> position = position_dodge(width = 0.9),
> size = 5,
> vjust = 0
> )+
> scale_y_continuous(
> limits = c(0, 1.1*ymax),
> labels = scales::percent
> )+
> theme_classic()
>
>
> The following geom_text will change the text labels color to red.
>
>
> geom_text(
> aes(label = scales::percent(percentage, accuracy = 0.1)),
> position = position_dodge(width = 0.9),
> #
> color = "red",
> #
> size = 5,
> vjust = 0
> )
>
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 17:52 de 19/08/21, bharat rawlley via R-help escreveu:
> > Hello
> > I have tried to create the following graph using ggplot2 using the
> following code -
> > ggplot(aes(x=year, y=percentage, group=gender, fill=gender), data =
> graph_text)+ geom_bar(position = 'dodge', stat='identity')+
> scale_y_continuous(expand = c(0,0))+ theme_classic()+
> geom_text(aes(label = percentage), size = 5, position =
> position_dodge(width = 0.5), vjust=0)
> >
> >
> > I wanted to ask
> > 1) In the last plot why is the label on top not showing up even when
> I save the image as a pdf?
> > 2) Is there anyway to make the labels appear in the middle of the
> bar? It is aligned to the left right now
> > 3) Is there any way to change the colour of the labels on the top of
> the bars?
> > Thank you!
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > ______________________________________________
> > R-help using r-project.org <mailto:R-help using r-project.org> mailing list -- To
> UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> <https://stat.ethz.ch/mailman/listinfo/r-help>
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> <http://www.R-project.org/posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
[[alternative HTML version deleted]]
More information about the R-help
mailing list