简体   繁体   中英

How to make barchart in R with a series?

For a table data as follows:

201109 a 222
201109 b 522
201109 c 522
201110 a 422
201110 b 422
201110 c 422
201111 a 122
201111 b 122
201111 c 122

How can we do a bar chart for each date on the x column ( 201111, 201110, 2011109) having 3 columns?

I am trying to read in the data:

atable <- read.table("data/a.tst", header=FALSE, sep="\t")

It is unclear how to reshape the table to use barplot.

Here's a sample answer. I'm not sure what you want the bar plot to look like, you could either stack or dodge the values.


library(tidyverse)

df1 <- structure(list(date = c("201109", "201109", "201109", "201110", 
"201110", "201110", "201111", "201111", "201111"), grp = c("a", 
"b", "c", "a", "b", "c", "a", "b", "c"), value = c("222", "522", 
"522", "422", "422", "422", "122", "122", "122")), row.names = c(NA, 
-9L), class = c("tbl_df", "tbl", "data.frame"))

df1 %>% 
  mutate(date = lubridate::ymd(paste0(date, "01"))) %>% 
  mutate(value = as.integer(value)) %>% 
  ggplot(aes(date, value, fill = grp))+
  geom_col(position = "dodge")

Created on 2020-11-12 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM