ggplot for a list?

38 views
Skip to first unread message

bic ton

unread,
Jun 12, 2018, 1:03:52 PM6/12/18
to ggplot2

​ Hi,

My data looks like this:

I want to plot histogram (like the attached one) and I know it can be done like this:

ggplot() + geom_histogram()

 

However, here I have a list and I am not sure how I do it. If I convert to dataframe, I will los the order that I want to keep in the graph  (1  and 3).

 

list(structure(list(Group.1 = structure(3:4, .Label = c("A",

"B", "C", "D", "E", "F"), class = "factor"), Pr1 = c(65, 75), Pr2 = c(54, 88), Prh = c(25, 5), SE = c(25, 75 )), row.names = c(NA, -2L), class = "data.frame"),NULL, structure(list( Group.1 = structure(3:4, .Label = c("A",

"B", "C", "D", "E", "F"), class = "factor"), Pr1 = c(81,4), Pr2 = c(66, 57),Prh = c(3,3), SE = c(8, 9)), row.names = c(NA,

-2L), class = "data.frame"))

Any help on this?

Thanks

Bick


Capture.PNG

Brandon Hurr

unread,
Jun 12, 2018, 2:31:12 PM6/12/18
to bic ton, ggplot2
Bick,

When you import the data, can you create the missing groupings? Trying to do it after the fact is really hard. Once you do that, it's super easy to compact this with dplyr::bind_rows().

> bic_data %>% dplyr::bind_rows()
  Group.1 Pr1 Pr2 Prh SE
1       C  65  54  25 25
2       D  75  88   5 75
3       C  81  66   3  8
4       D   4  57   3  9


If you put a group variable in:
bic_data2 <- list(structure(list(super_group = c(1,1), Group.1 = structure(3:4, .Label = c("A",

"B", "C", "D", "E", "F"), class = "factor"), Pr1 = c(65, 75), Pr2 = c(54, 88), Prh = c(25, 5), SE = c(25, 75 )), row.names = c(NA, -2L), class = "data.frame"), NULL, structure(list(super_group = c(3,3), Group.1 = structure(3:4, .Label = c("A",


"B", "C", "D", "E", "F"), class = "factor"), Pr1 = c(81,4), Pr2 = c(66, 57),Prh = c(3,3), SE = c(8, 9)), row.names = c(NA,

-2L), class = "data.frame"))

bic_data2 %>%
bind_rows() %>%
ggplot(., aes(x = Group.1, y = Pr1, fill = Group.1)) +
geom_bar(stat = "identity") +
facet_grid(. ~ super_group)
Screen Shot 2018-06-12 at 11.30.11 AM.png
HTH
B



--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages