Home > Enterprise >  How Can I plot a table with multiple columns in R
How Can I plot a table with multiple columns in R

Time:11-30

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?

CodePudding user response:

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))
  •  Tags:  
  • r
  • Related