Dotplot with 3 variables help

Hi.

I am a user of Stata and I am not used to R, but I need to create these 3 plot using ggplot2.
Can anyone help me?

The code in Stata is at follows:
graph dot interesedu, by(cat_edad) over(year)

Hi, can you provide a reproducible example?

An example of something similar using default data is:

library(ggplot2)

dplyr::glimpse(iris)
#> Rows: 150
#> Columns: 5
#> $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.~
#> $ Sepal.Width  <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.~
#> $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.~
#> $ Petal.Width  <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.~
#> $ Species      <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s~

ggplot(iris, aes(Sepal.Length, Petal.Length)) +
  geom_point() +
  facet_wrap(~Species)

Created on 2021-12-20 by the reprex package (v2.0.1)

Yes, sorry.

This is my data

That's not a reproducible example; I can't copy and paste a static image into RStudio!

Please provide the output of dput(your_data).


tibble2 <- structure(list(cat_age = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L), .Label = c("18-30", "31-65", "66 or more"
), class = "factor"), year = c(2002, 2004, 2006, 2008, 2010, 
2012, 2014, 2016, 2018, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 
2016, 2018, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018
), avg_interest = c(3.08745247148289, 2.88695652173913, 3.05744125326371, 
3.00212765957447, 2.97746478873239, 2.77083333333333, 2.6872852233677, 
2.67518248175182, 2.63934426229508, 3.03887688984881, 2.79934924078091, 
2.89750957854406, 2.92439862542955, 2.88712522045855, 2.79040404040404, 
2.62489270386266, 2.67104183757178, 2.65768463073852, 3.29711751662971, 
3.28104575163399, 3.34417344173442, 3.27985074626866, 3.06796116504854, 
3.0058651026393, 2.93979057591623, 2.84696569920844, 2.99399399399399
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-27L))

´´´
library(ggplot2)

tibble2 <- structure(list(cat_age = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
                                                1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
                                                3L, 3L, 3L, 3L, 3L), .Label = c("18-30", "31-65", "66 or more"
                                                ), class = "factor"), year = c(2002, 2004, 2006, 2008, 2010, 
                                                                               2012, 2014, 2016, 2018, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 
                                                                               2016, 2018, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018
                                                ), avg_interest = c(3.08745247148289, 2.88695652173913, 3.05744125326371, 
                                                                    3.00212765957447, 2.97746478873239, 2.77083333333333, 2.6872852233677, 
                                                                    2.67518248175182, 2.63934426229508, 3.03887688984881, 2.79934924078091, 
                                                                    2.89750957854406, 2.92439862542955, 2.88712522045855, 2.79040404040404, 
                                                                    2.62489270386266, 2.67104183757178, 2.65768463073852, 3.29711751662971, 
                                                                    3.28104575163399, 3.34417344173442, 3.27985074626866, 3.06796116504854, 
                                                                    3.0058651026393, 2.93979057591623, 2.84696569920844, 2.99399399399399
                                                )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                            -27L))

# basic
ggplot(tibble2, aes(y = year, x = avg_interest)) +
  geom_point() + 
  facet_wrap(~cat_age)

# close to original
ggplot(tibble2, aes(y = year, x = avg_interest)) +
  geom_point() + 
  facet_wrap(~cat_age, ncol = 2) +
  scale_y_reverse(breaks = seq(2000,2050,2)) +
  theme_bw() +
  theme(aspect.ratio = .6,
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.y = element_line(linetype = 2),
        plot.background = element_rect(fill = "azure", color = NA),
        strip.background = element_rect(fill = "azure2", color = NA),
        plot.caption = element_text(hjust = 0)) +
  labs(y = NULL, x = "mean of interestedu",
       caption = "Graphs by cat_edad")

Created on 2021-12-20 by the reprex package (v2.0.1)

2 Likes

Thank you very much!!!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.