Reputation: 1111
Could you help me with the following question?
I would like to generate a scatter plot using plot
function, where the x-axis
would be columns DR01_PV to DR06_PV and the y-axis
would be column D1. Both of the PV variable
library(dplyr)
data <- structure(
list(Id=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
date1 = c("2021-06-20","2021-06-20","2021-06-20","2021-06-20","2021-06-20",
"2021-06-20","2021-06-20","2021-06-20","2021-06-20","2021-06-20","2021-06-20",
"2021-06-20","2021-06-20","2021-06-20","2021-06-20","2021-06-20","2021-06-20",
"2021-06-20","2021-06-20","2021-06-20","2021-06-20"),
date2 = c("2021-07-01","2021-07-01","2021-07-01","2021-07-01","2021-04-02",
"2021-04-02","2021-04-02","2021-04-02","2021-04-02","2021-04-02","2021-04-03",
"2021-04-03","2021-04-03","2021-04-03","2021-04-03","2021-04-08","2021-04-08",
"2021-04-09","2021-04-09","2021-04-10","2021-04-10"),
Week= c("Thursday","Thursday","Thursday","Thursday","Friday","Friday","Friday","Friday",
"Friday","Friday","Saturday","Saturday","Saturday","Saturday","Saturday","Thursday",
"Thursday","Friday","Friday","Saturday","Saturday"),
DTPE = c("Ho","Ho","Ho","Ho","","","","","","","","","","","","","","","","Ho","Ho"),
D1 = c(8,1,9,3,5,4,7,6,3,8,2,3,4,6,7,8,4,2,6,2,3), DR01 = c(4,1,4,3,3,4,3,6,3,7,2,3,4,6,7,8,4,2,6,7,3),
DR02 = c(8,1,4,3,3,4,1,6,3,7,2,3,4,6,7,8,4,2,6,2,3), DR03 = c(7,5,4,3,3,4,1,5,3,3,2,3,4,6,7,8,4,2,6,4,3),
DR04= c(4,5,6,7,3,2,7,4,2,1,2,3,4,6,7,8,4,2,6,4,3),DR05 = c(9,5,4,3,3,2,1,5,3,7,2,3,4,7,7,8,4,2,6,4,3),
DR06 = c(5,4,3,3,6,2,1,9,3,7,2,3,4,7,7,8,4,2,6,4,3)),
class = "data.frame", row.names = c(NA, -21L))
data<-data %>%
group_by(date2) %>%
select(D1:DR06) %>%
summarise_all(sum)
x<-subset(data, select = DR01:DR06)
x<-cbind(data, setNames(data$D1 - x, paste0(names(x), "_PV")))
PV<-select(x, date2, D1, ends_with("PV"))
PV
date2 D1 DR01_PV DR02_PV DR03_PV DR04_PV DR05_PV DR06_PV
1 2021-04-02 33 7 9 14 14 12 5
2 2021-04-03 22 0 0 0 0 -1 -1
3 2021-04-08 12 0 0 0 0 0 0
4 2021-04-09 8 0 0 0 0 0 0
5 2021-04-10 5 -5 0 -2 -2 -2 -2
6 2021-07-01 21 9 5 2 -1 0 6
Upvotes: 0
Views: 71
Reputation: 46908
either make a facet:
lvl = grep("_PV",colnames(PV),value=TRUE)
par(mfrow=c(1,6))
for(i in lvl){
plot(PV[[i]],PV[["D1"]],xlab=i,ylab="D1")
}
you need to pivot your data long:
par(mfrow=c(1,1))
PV_long = PV %>%
select(D1:DR06_PV) %>%
pivot_longer(-D1) %>%
plot(D1 ~ value,col=factor(name,levels=lvl),data=.)
legend("topleft",fill=palette(),lvl,cex=0.7)
Upvotes: 0
Reputation: 15123
I made plot with every DR0...
's are in one plot. If not, please let me know.
PV %>%
select(- date2) %>%
reshape2::melt(.,id.vars = "D1") %>%
ggplot(aes(value, D1, group = variable, color = variable)) +
geom_point()
Upvotes: 1