esem
esem

Reputation: 173

Plot a multivariate histogram in R

I would like to plot 6 different variables with their corresponding calculated statistical data. The following dataframe may serve as an example

     X   aggr_a  aggr_b  count
  <chr>   <dbl>   <dbl>  <dbl>
1 A      470676  594423  58615
2 B      549142  657291  67912
3 C      256204  311723  26606
4 D      248256  276593  40201
5 E     1581770 1717788 250553
6 F     1932096 2436769 385556

I would like to plot each row as category with its statistics as histogram bins. The desired output is

enter image description here

May I use ggplots for this kind of graphs?

All the available resources seem to cover the uni variate case only.

Upvotes: 0

Views: 1218

Answers (1)

AntoniosK
AntoniosK

Reputation: 16121

library(tidyverse)

df = read.table(text = "
X   aggr_a  aggr_b  count
A      470676  594423  58615
B      549142  657291  67912
C      256204  311723  26606
D      248256  276593  40201
E     1581770 1717788 250553
F     1932096 2436769 385556
", header=T)

df %>%
  gather(type,value,-X) %>%           # reshape dataset
  ggplot(aes(X,value,fill=type))+
  geom_bar(position = "dodge", stat = "identity")

enter image description here

Upvotes: 3

Related Questions