0
Follow
2
View

How to plot a grouped geom_bar plot having a dataframe?

GDL5211dht 注册会员
2023-01-26 09:47

We may use plot from base R

barplot(t(df1), beside = TRUE, col = c("blue", "orange", "grey", 
       "yellow", "lightblue" ))

-output


Or if we need a ggplot/plotly

library(ggplot2)
library(dplyr)
library(tidyr)
library(plotly)
library(tibble)
p <- df1 %>% 
   rownames_to_column('rn') %>%
   pivot_longer(cols = -rn) %>%
   ggplot(aes(x = rn, y = value, fill = name)) +
        geom_col(position = 'dodge') + 
     theme_bw()
ggplotly(p)

-output

data

df1 <- structure(list(A = c(65, 9, 7, 70, 9), B = c(23, 4, 5, 53, 2), 
    C = c(42, 5, 2, 17, 7), D = c(51, 7, 5, 57, 5), E = c(14, 
    2, 2, 13, 4)), class = "data.frame", row.names = c("AAA", 
"BBB", "CCC", "DDD", "EEE"))

About the Author

Question Info

Publish Time
2023-01-26 09:47
Update Time
2023-01-26 09:47