-1

I have a function that gives me the percentage of each row, but I cant plot the Table. My function is this:

porcentaje.preguntas <- df %>%
  group_by(Localidad) %>%
  select(Localidad, starts_with("X")) %>%
  summarise(across(where(is.numeric), ~ scales::percent(mean(.x))))

And the output its something like this:

# A tibble: 309 x 73
   Localidad  X2.1  X2.2  X2.3  X2.4  X2.5  X2.6  X2.7  X2.8  X2.9  X2.1.1 X2.11 X2.12 X3.1  X3.2  X5.1  X5.2  X5.3 
   <chr>      <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>  <chr> <chr> <chr> <chr> <chr> <chr> <chr>
 1 0001CIUDA~ 100%  100%  100%  100%  100%  100%  100%  100%  100%  100%   100%  100%  100%  100%  100%  100%  100% 
 2 01001AGUA~ 100%  99%   99%   100%  100%  98%   100%  100%  100%  100%   100%  100%  100%  100%  100%  100%  100% 
 3 01003CALV~ 100%  100%  100%  100%  100%  100%  100%  100%  100%  100%   100%  100%  100%  100%  100%  100%  100% 
 4 01005JESÚ~ 100%  100%  100%  100%  100%  100%  100%  100%  100%  100%   100%  100%  100%  100%  100%  100%  100% 
 5 01006PABE~ 100%  100%  100%  100%  100%  89%   100%  100%  100%  100%   100%  100%  100%  100%  100%  100%  100% 
 6 01007RINC~ 100%  100%  100%  100%  100%  100%  100%  100%  100%  100%   100%  100%  100%  100%  100%  100%  100% 
 7 01011SAN ~ 100%  100%  100%  100%  100%  100%  100%  100%  100%  100%   100%  100%  100%  100%  100%  100%  100% 
 8 02001ENSE~ 100%  100%  100%  100%  100%  96%   100%  100%  100%  100%   100%  100%  100%  100%  99%   97%   100% 
 9 02002MEXI~ 100%  100%  100%  100%  100%  99%   100%  100%  100%  100%   100%  100%  100%  99%   99%   97%   100% 
10 02003TECA~ 100%  96%   96%   100%  100%  78%   100%  96%   100%  100%   100%  100%  100%  96%   96%   100%  100% 
# ... with 299 more rows, and 55 more variables

How Can I plot this Table with all the columns?

camille
  • 16,432
  • 18
  • 38
  • 60
Juan Jesus
  • 23
  • 6
  • 2
    Convert to long format and plot i.e. `library(tidyr);porcentaje.preguntas %>% pivot_longer(cols = -Localidad) %>% ggplot(aes(...` – akrun Nov 29 '21 at 18:01
  • 2
    [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with. Right now we can only guess as to what exactly you want to plot, how, and with what tools – camille Nov 29 '21 at 18:12
  • Thanks for answering @camille I just want to plot the table, because its a school project, so any idea of any plot would be appreciated – Juan Jesus Nov 29 '21 at 18:21
  • @akrun can you be more specific? Thanks for answering – Juan Jesus Nov 29 '21 at 18:25
  • 2
    The type of plot really depends on the context and purpose, and isn't something that we can decide for you, especially without knowing anything about what this data symbolizes. You could easily chop this up into a dozen different types of plots; which one is right depends on a whole lot of factors that aren't included in this question – camille Nov 29 '21 at 18:56
  • @camille Yeah I know, This data symbolizes the percentage of questions answered grouped by each Locality known as "Localidad" but my problem is that I dont know which plot to use because that table in specific have more than 300 rows and 75 columns, so Its impossible to plot that amount of data. – Juan Jesus Nov 29 '21 at 19:02
  • All of that makes this too broad a question for the purposes of SO. I'd recommend reading up on data visualization and chart types, while thinking through the purpose of your visualization. [The Data Viz Catalog](https://datavizcatalogue.com/) and [R Graph Gallery](https://www.r-graph-gallery.com/) are good places to start. If I'm being stubborn in insisting that you think about & figure out what you're plotting and why, it's because I do this for a living and it's really not useful to just toss out the type of plot I think you should make with no knowledge of the purpose – camille Nov 29 '21 at 19:22

1 Answers1

1

As recommended, you need to convert the data from wide to long format, then you can plot the data. As far as the type of plot, it depends on what you are looking for.

library(tidyverse)

porcentaje.preguntas %>% 
  tidyr::pivot_longer(cols = !Localidad) %>% 
  ggplot(aes(fill = name, y = value, x = Localidad)) +
  geom_bar(position="dodge", stat="identity")

enter image description here

If you don't care about row names, then you could do something like this.

porcentaje.preguntas %>% 
  tidyr::pivot_longer(cols = !Localidad) %>% 
  ggplot(aes(fill = Localidad, y = value, x = Localidad)) +
  geom_bar(position="dodge", stat="identity") + 
  theme(legend.position = "none")

enter image description here

Data

porcentaje.preguntas <- structure(list(Localidad = c("0001CIUDA~", "01001AGUA~", "01003CALV~", 
"01006PABE~", "01007RINC~", "01011SAN ~", "02001ENSE~", "02002MEXI~", 
"02003TECA~"), X2.1 = c("100%", "100%", "100%", "100%", "100%", 
"100%", "100%", "100%", "100%"), X2.2 = c("100%", "99%", "100%", 
"100%", "100%", "100%", "100%", "100%", "96%"), X2.3 = c("100%", 
"99%", "100%", "100%", "100%", "100%", "100%", "100%", "96%"), 
    X2.4 = c("100%", "100%", "100%", "100%", "100%", "100%", 
    "100%", "100%", "100%"), X2.5 = c("100%", "100%", "100%", 
    "100%", "100%", "100%", "100%", "100%", "100%"), X2.6 = c("100%", 
    "98%", "100%", "89%", "100%", "100%", "96%", "99%", "78%"
    ), X2.7 = c("100%", "100%", "100%", "100%", "100%", "100%", 
    "100%", "100%", "100%"), X2.8 = c("100%", "100%", "100%", 
    "100%", "100%", "100%", "100%", "100%", "96%"), X2.9 = c("100%", 
    "100%", "100%", "100%", "100%", "100%", "100%", "100%", "100%"
    ), X2.1.1 = c("100%", "100%", "100%", "100%", "100%", "100%", 
    "100%", "100%", "100%"), X2.11 = c("100%", "100%", "100%", 
    "100%", "100%", "100%", "100%", "100%", "100%"), X2.12 = c("100%", 
    "100%", "100%", "100%", "100%", "100%", "100%", "100%", "100%"
    ), X3.1 = c("100%", "100%", "100%", "100%", "100%", "100%", 
    "100%", "100%", "100%"), X3.2 = c("100%", "100%", "100%", 
    "100%", "100%", "100%", "100%", "99%", "96%"), X5.1 = c("100%", 
    "100%", "100%", "100%", "100%", "100%", "99%", "99%", "96%"
    ), X5.2 = c("100%", "100%", "100%", "100%", "100%", "100%", 
    "97%", "97%", "100%"), X5.3 = c("100%", "100%", "100%", "100%", 
    "100%", "100%", "100%", "100%", "100%")), class = "data.frame", row.names = c(NA, 
-9L))
AndrewGB
  • 16,126
  • 5
  • 18
  • 49
  • Thank you so much for answering, How can I select all my Columns? Because In my table are 73 columns, so when Im plotting i don't know how to fill aes() – Juan Jesus Nov 29 '21 at 18:35
  • @JuanJesus I added another option if you don't care about the row names. But it really depends on what you want the desired output to be. – AndrewGB Nov 29 '21 at 18:42
  • 1
    Thank you so much, In my dataframe its unreadable because im working with more than 20k of variables, so maybe I will separate or do something else. But thank you so much it resolves my problem (: – Juan Jesus Nov 29 '21 at 18:51