简体   繁体   中英

Bar plot with multiple Ids on the same dates

Edit: Here's the data:

date<-c("20130107", "20130109", "20130111", "20130111", "20130113", "20130114", "20130114", "20130122", "20130125", "20130125", "20130128")
ID<-c("Blue","Red","Red","Red","Red","Red","White","Green","Black","Purple","Purple")
date_ID<-c("20130107 Blue", "20130109 Red", "20130111 Red", "20130111 Red", "20130113 Red", "20130114 Red", "20130114 White", "20130122 Green", "20130125 Black", "20130125 Purple", "20130128 Purple")
perc_yes<-c(-0.10394265, -1.00000000, -1.00000000, -1.00000000, -1.00000000, -1.00000000, -0.40425532, -0.09297913, -1.00000000, -0.17864924, -0.12353401)
perc_no<-c(0.8960573, 0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.5957447, 0.9070209, 0.0000000, 0.8213508, 0.8764660)
data<-data.frame(date,ID,date_ID,perc_yes,perc_no)

I have a dataframe that looks like:

    date        ID          date_ID     perc_yes    perc_no
1   20130107    Blue    20130107 Blue   -0.10394265 0.8960573
2   20130109    Red     20130109 Red    -1.00000000 0.0000000
3   20130111    Red     20130111 Red    -1.00000000 0.0000000
4   20130111    Red     20130111 Red    -1.00000000 0.0000000
5   20130113    Red     20130113 Red    -1.00000000 0.0000000
6   20130114    Red     20130114 Red    -1.00000000 0.0000000
7   20130114    White   20130114 White  -0.40425532 0.5957447
8   20130122    Green   20130122 Green  -0.09297913 0.9070209
9   20130125    Black   20130125 Black  -1.00000000 0.0000000
10  20130125    Purple  20130125 Purple -0.17864924 0.8213508
11  20130128    Purple  20130128 Purple -0.12353401 0.8764660

It lists the date, ID, and then the date and ID which is how they were grouped, percentage yes and percentage no. I'd like to make a bar graph by date and ID that is in chronological order and the bars are grouped together if they are on the same date but different IDs. The percentage yes's are negative as I'd like them to fall in the negative y direction directly underneath the percentage no's.

I tried barplot with no success as there's multiple data points and stacking. Is there a way to add multiple barplot points like with lines() in the plot function? How would I go about plotting the data? Would it be better to use ggplot (of which I am somewhat unfamiliar)?

I guess ggplot2 might be more flexible, but I have no idea on it. Maybe the following -using only barplot - might be a bit helpful:

#turn `data$date` to actual date
data$date <- as.Date(data$date, format = "%Y%m%d")

#sort by date
data <- data[order(data$date),]

#`space` argument of barplot; 
#to group together same dates based on their difference in days
#see ?diff.Date
space. <- diff(data$date) + 0.5
space. <- c(space.[1], space.)

#plot no's
barplot(data$perc_no, names.arg = paste0(data$date, "\n", data$ID), cex.names = 0.7, 
        ylim = c(range(c(data$perc_yes, data$perc_no))),
        col = rgb(1,0,0,1/3), space = space.)

#plot yes'
barplot(data$perc_yes, col = rgb(0,0,1,1/3), , space = space., add = T)

The plot looks like:

条形图

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