[R] Help needed with ggplot2

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Sun Aug 22 01:38:33 CEST 2021


The code supplied is not proper for several reasons including not being on multiple lines properly and use of variables not defined.

"percentage" is a field in data.frame "email" not in "graph_text" and of course you need to load libraries properly to use the functions.

I rewrote and fixed a few errors to look like this:

library(tidyverse)

graph_text <- structure(list(percentage = c(57.14, 29.76, 69.32, 28.41, 57.89, 34.21, 58.59, 33.33, 48.42, 42.11, 59.77, 29.89, 72.13, 18.03, 53.33, 33.33, 55.1, 40.82, 46.55, 37.93),
                             year = c(2020L, 2020L, 2019L, 2019L, 2018L, 2018L, 2017L, 2017L, 2016L, 2016L, 2015L, 2015L, 2014L, 2014L, 2013L, 2013L, 2012L, 2012L, 2011L, 2011L), 
                             gender = c("male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female")), 
                        class = "data.frame", 
                        row.names = c(NA, -20L))

ymax <- max(graph_text$percentage)

ggplot(data = graph_text, 
       aes(x=year, 
           y=percentage, 
           color = gender, 
           fill=gender)) +  
  geom_bar(position = 'dodge', 
           stat='identity') +  
  theme_classic() +  
  geom_text(aes(label = percentage), 
            size = 4, 
            position = position_dodge(width = 1.1), 
            vjust=-0.2) +   
  scale_y_continuous(limits=c(0, 1.4*ymax))


And interestingly, it showed the years as 2010.0, 2012.5 and every 2.5 years thereafter, like the first version you showed. 

What you are asking for is straightforward enough if you do some simple queries on how to set the x axis up. You want integers shown as it they were years that presumably start with some year near the minimum and continue toward the maximum.  Do you want every year or just every N years?

One low-tech solution is to change year from an integer to a factor of integers or characters like this:

graph_text$year <- as.factor(graph_text$year)

The labels now look reasonable.

I won't solve your other issues but there are documented ways. Bold text is an example that can be changed in many places. In this case, note the addition to the following part from above:

fontface="bold

as in:

  geom_text(aes(label = percentage), 
            size = 4, 
            position = position_dodge(width = 1.1), 
            vjust=-0.2,
            fontface="bold") +








-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of bharat rawlley via R-help
Sent: Saturday, August 21, 2021 6:24 PM
To: Bert Gunter <bgunter.4567 using gmail.com>
Cc: R-help Mailing List <r-help using r-project.org>
Subject: Re: [R] Help needed with ggplot2

 Thank you, I have tried to do a better job here - 



Data - 
email <- structure(list(percentage = c(57.14, 29.76, 69.32, 28.41, 57.89,                                        34.21, 58.59, 33.33, 48.42, 42.11, 59.77, 29.89, 72.13, 18.03,                                        53.33, 33.33, 55.1, 40.82, 46.55, 37.93), year = c(2020L, 2020L,                                                                                           2019L, 2019L, 2018L, 2018L, 2017L, 2017L, 2016L, 2016L, 2015L,                                                                                           2015L, 2014L, 2014L, 2013L, 2013L, 2012L, 2012L, 2011L, 2011L                                       ), gender = c("male", "female", "male", "female", "male", "female",                                                      "male", "female", "male", "female", "male", "female", "male",                                                      "female", "male", "female", "male", "female", "male", "female"                                       )), class = "data.frame", row.names = c(NA, -20L))



Code - 
ymax <- max(graph_text$percentage)ggplot(aes(x=year, y=percentage, color = gender, fill=gender, data = graph_text)+  geom_bar(position = 'dodge', stat='identity')+  theme_classic()+  geom_text(aes(label = percentage), size = 4, position = position_dodge(width = 1.1), vjust=-0.2) +   scale_y_continuous(limits=c(0, 1.4*ymax))



Session info - 
R version 4.1.0 (2021-05-18)Platform: x86_64-w64-mingw32/x64 (64-bit)Running under: Windows 10 x64 (build 19042)
Matrix products: default
locale:[1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252    LC_MONETARY=English_India.1252[4] LC_NUMERIC=C                   LC_TIME=English_India.1252    
attached base packages:[1] stats     graphics  grDevices utils     datasets  methods   base     
loaded via a namespace (and not attached): [1] fansi_0.5.0      assertthat_0.2.1 dplyr_1.0.6      crayon_1.4.1     utf8_1.2.1       [6] grid_4.1.0       R6_2.5.0         DBI_1.1.1        lifecycle_1.0.0  gtable_0.3.0    [11] magrittr_2.0.1   scales_1.1.1     ggplot2_3.3.3    pillar_1.6.1     rlang_0.4.11    [16] generics_0.1.0   vctrs_0.3.8      ellipsis_0.3.2   tools_4.1.0      glue_1.4.2      [21] purrr_0.3.4      munsell_0.5.0    compiler_4.1.0   pkgconfig_2.0.3  colorspace_2.0-1[26] tidyselect_1.1.1 tibble_3.1.2    


I have the following questions - 
Q1 How can I make the years appear on x axis as 2011, 2012, 2013, 2014 and so on (earlier they were showing up as 2012.5 etc. which has disappeared now for reason I do not know)
Q2 Is there any way to create a small gap between the red and blue bars for aesthetic purposes
Q3 Is there anyway to make the text on top of the bars bolder or thicker? 
Thank you




    On Saturday, 21 August, 2021, 05:57:09 pm GMT-4, Bert Gunter <bgunter.4567 using gmail.com> wrote:  
 
 See ?dput for how to provide a reproducible example (a reprex). Or see here:
https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

It will improve your chance of getting a helpful and quick response.

Cheers,
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Sat, Aug 21, 2021 at 2:47 PM bharat rawlley via R-help
<r-help using r-project.org> wrote:
>
>
> Hello, on using the following code for the following data, the graph I get has an x axis where years are mentioned as 2012.5, 2017.5 etc.
>
> I have the following questions -
> Q1 How can I make the years on x axis as 2011, 2012, 2013, 2014 and so on..
> Q2 Is there any way to create a small gap between the red and blue bars for aesthetic purposes
> Q3 Is there anyway to make the text on top of the bars bolder or thicker?
> Thank you
>
> code -
> ymax <- max(graph_text$percentage)ggplot(aes(x=year, y=percentage, color = gender, fill=gender), data = graph_text)+  geom_bar(position = 'dodge', stat='identity')+  theme_classic()+  geom_text(aes(label = percentage), size = 4, position = position_dodge(width = 1.1), vjust=-0.2) +  scale_y_continuous(limits=c(0, 1.4*ymax))
>
>
> | 57.14 | 2020 |  male |
> | 29.76 | 2020  | female |
> | 69.32 | 2019  | male |
> | 28.41 | 2019 |  female |
> | 57.89 | 2018 |  male |
> | 34.21 | 2018  | female |
> | 58.59 | 2017 |  male |
> | 33.33 | 2017  | female |
> | 48.42 | 2016  | male |
> | 42.11 | 2016 |  female |
> | 59.77 | 2015 |  male |
> | 29.89 | 2015 |  female |
> | 72.13 | 2014 |  male |
> | 18.03 | 2014 |  female |
> | 53.33 | 2013 |  male |
> | 33.33 | 2013 |  female |
> | 55.1 | 2012 |  male |
> | 40.82 | 2012  | female |
> | 46.55 | 2011  | male |
> | 37.93 | 2011  | female |
>
>
>
>
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
  



More information about the R-help mailing list