Related
this question is linked to a previous one answered by #Rui Barradas and #Duck, but i need more help. Previous link here:
how do i vectorise (automate) plot creation in R
Basically, I need to combine 3 datasets into one plot with a secondary y axis. All datasets need to be split by SITENAME and will facet wrap by Sampling.Year. I am using split and sapply. Being facet wrap the plots look something like this:
However, i'm now trying to add the two other data sources into the plots, to look something like this:
But i am struggling to add the two other data sources and get them to split by SITENAME. Her is my code so far...
Record plot format as a function to be applied to a split list df (ideally 'df' would be added as geom_line with a secondary y axis, and 'FF_start_dates' will be added as a vertical dashed line):
SITENAME_plot <- function(AllDates_TPAF){
ggplot(AllDates_TPAF, aes(DATE, Daily.Ave.PAF)) +
geom_point(aes(colour = Risk), size = 3) +
scale_colour_manual(values=c("Very Low" = "dark green","Low" = "light green",
"Moderate" = "yellow", "High" = "orange", "Very High" = "red"), drop = FALSE) +
labs(x = "Month", y = "Total PAF (% affected)") +
scale_x_date(breaks = "1 month", labels = scales::date_format("%B")) +
facet_wrap(~Sampling.Year, ncol = 1, scales = "free")+
scale_y_continuous(limits = c(0, 100), sec.axis = sec_axis(~., name = "Water level (m)")) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
theme(legend.text=element_text(size=15)) +
theme(axis.text=element_text(size=15),
axis.title=element_text(size=15,face="bold")) +
guides(color = guide_legend(reverse = TRUE))+
theme_bw() +
ggtitle(unique(AllDates_TPAF$SITENAME))
}
plot write function:
SITENAME_plot_write <- function(name, g, dir = "N:/abc/"){
flname <- file.path(dir, name)
flname <- paste0(flname, ".jpg")
png(filename = flname, width = 1500, height = 1000)
print(g)
dev.off()
flname
}
Apply function to list split by SITENAME:
sp1 <- split(AllDates_TPAF, AllDates_TPAF$SITENAME)
gg_list <- sapply(sp1, SITENAME_plot, simplify = FALSE)
mapply(SITENAME_plot_write, names(gg_list), gg_list, MoreArgs = list(dir = getwd()))
dev.off()
I have uploaded samples of all 3 datasets here: Sample Data
Apologies for not using gsub but there was too much data and I couldn't get it to work properly
thanks in advance for any help you can give, even if it is just to point me towards a web tutorial of some kind.
You can try next code. I used the data you shared. Just be careful with names of all datasets. Ideally, the key columns as DATE and Sampling.Year should be present in all dataframes before making the split. Also some variables as Risk was absent so I added an example var with same name. Here the code, I added a function for the plot you want:
library(tidyverse)
library(readxl)
#Data
df1 <- read_excel('Sample data.xlsx',1)
#Create var
df1$Risk <- c(rep(c("Very Low","Low","Moderate","High","Very High"),67),"Very High")
#Other data
df2 <- read_excel('Sample data.xlsx',2)
df3 <- read_excel('Sample data.xlsx',3)
#Split 1
L1 <- split(df1,df1$SITENAME)
L2 <- split(df2,df2$SITENAME)
L3 <- split(df3,df3$`Site Name`)
#Function to create plots
myplot <- function(x,y,z)
{
#Merge x and y
#Check for duplicates and avoid column
y <- y[!duplicated(paste(y$DATE,y$Sampling.Year)),]
y$SITENAME <- NULL
xy <- merge(x,y,by.x = c('Sampling.Year','DATE'),by.y = c('Sampling.Year','DATE'),all.x=T)
#Format to dates
xy$DATE <- as.Date(xy$DATE)
#Scale factor
scaleFactor <- max(xy$Daily.Ave.PAF) / max(xy$Height)
#Rename for consistency in names
names(z)[4] <- 'DATE'
#Format date
z$DATE <- as.Date(z$DATE)
#Plot
#Plot
G <- ggplot(xy, aes(DATE, Daily.Ave.PAF)) +
geom_point(aes(colour = Risk), size = 3) +
scale_colour_manual(values=c("Very Low" = "dark green","Low" = "light green",
"Moderate" = "yellow", "High" = "orange", "Very High" = "red"), drop = FALSE) +
scale_x_date(breaks = "1 month", labels = scales::date_format("%b %Y")) +
geom_line(aes(x=DATE,y=Height*scaleFactor))+
scale_y_continuous(name="Total PAF (% affected)", sec.axis=sec_axis(~./scaleFactor, name="Water level (m)"))+
labs(x = "Month") +
geom_vline(data = z,aes(xintercept = DATE),linetype="dashed")+
facet_wrap(~Sampling.Year, ncol = 1, scales = "free")+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
theme(legend.text=element_text(size=15)) +
theme(axis.text=element_text(size=15),
axis.title=element_text(size=15,face="bold")) +
guides(color = guide_legend(reverse = TRUE))+
theme_bw() +
ggtitle(unique(xy$SITENAME))
return(G)
}
#Create a list of plots
Lplots <- mapply(FUN = myplot,x=L1,y=L2,z=L3,SIMPLIFY = FALSE)
#Now format names
vnames <- paste0(names(Lplots),'.png')
mapply(ggsave, Lplots,filename = vnames,width = 30,units = 'cm')
You will end up with plots like these saved in your dir:
Some dashed lines do not appear in plots because they were not present in the data you provided.
For the following data set, I would like to plot for each variable and color each 10th observations differently. I can do it using the R base. I want to learn how to do it using the ggplot2?
dput(mydata)
structure(list(beta0_C1 = c(5.90722120539152, 5.89025566996191,
5.88591520258904, 5.86911167649919, 5.93772460437405, 5.92985640353594,
5.89150365752453, 5.99046628686212, 5.91548006074821, 5.91571832976612,
5.88437484241154, 5.92092513223357, 5.98978050584774, 5.91152552752889,
5.91235823292462, 5.87961960044268, 5.84048698713552, 5.85484766204026,
5.94002829943904, 5.8844367778216, 5.90201348639369, 5.91220967575205,
5.90010933186624, 5.9187781795242, 5.85506764080697, 5.90103565341373,
5.88527143992961, 5.90218851192948, 5.90118162849608, 5.93147588185271
), beta1_C1 = c(0.389473200070741, 0.386495525456602, 0.401277295631578,
0.400952009358693, 0.376727640651344, 0.380365338054745, 0.393444927288697,
0.351041363714069, 0.393194356572458, 0.393448101768608, 0.398884551136789,
0.399458966787235, 0.357436746423815, 0.393782316102096, 0.387154169967002,
0.400838223362088, 0.404272252119662, 0.407427775176583, 0.379704250022161,
0.388842664781329, 0.382202010301184, 0.401354531881688, 0.391184010553641,
0.390280828053183, 0.402135923802544, 0.384344141458216, 0.405409447440106,
0.391719398951194, 0.398025625260563, 0.361822915989445), beta2_C1 = c(-0.0214886993465096,
-0.020723519439664, -0.0224612526333316, -0.0218187226687474,
-0.0200324040063121, -0.0208421378685671, -0.0218756660346625,
-0.0182499666400075, -0.0222765863213226, -0.022242845613047,
-0.0222033291270054, -0.0231570312767931, -0.0189429585905841,
-0.0221017468740293, -0.0209327798783444, -0.022409049257, -0.021698958175968,
-0.0225601087054418, -0.020928341508875, -0.0214668830626075,
-0.0205872002686706, -0.0233768022702472, -0.021755967293395,
-0.0218442145294776, -0.0222514480818199, -0.0212195394692002,
-0.0232109717283908, -0.0214814999754984, -0.0225124468437127,
-0.0187033387452614), beta0_C2 = c(6.50537199380546, 6.43626630601952,
6.44460360859128, 6.44788878017196, 6.49678676895955, 6.48474789770674,
6.5459727637079, 6.37593806532098, 6.39492158034295, 6.44497331914909,
6.3888816168562, 6.49660574813212, 6.45922901141938, 6.40080765767324,
6.37918638201668, 6.49354321098856, 6.47057962920788, 6.55699741431025,
6.56617313133218, 6.54271932949381, 6.44608000042182, 6.45333777656105,
6.67458442747556, 6.48420983182487, 6.59919337271637, 6.46645685814734,
6.46171236062657, 6.52625058117578, 6.51177045919728, 6.49897849935538
), beta1_C2 = c(-0.370455826326915, -0.338852275811034, -0.340671118342601,
-0.339888681238265, -0.36934391822867, -0.357194169746804, -0.415966150286963,
-0.349051278947586, -0.358209379291251, -0.371785518417424, -0.349725822847608,
-0.368220986471866, -0.327425879655177, -0.336993142255552, -0.328859493371605,
-0.347764105375218, -0.329761787134926, -0.37935820670654, -0.400211161919931,
-0.408699321227288, -0.357590345066542, -0.376548827126353, -0.44672514669147,
-0.353840422053319, -0.421912098450693, -0.371491468175642, -0.354864346664247,
-0.39139246919467, -0.379006372881295, -0.372492936183765), beta2_C2 = c(0.039728365796445,
0.0368393936404604, 0.0375019672690036, 0.0375019364609944, 0.0403444583999664,
0.0378627636833333, 0.0446717245407897, 0.0377538641609231, 0.039662572899695,
0.0408055348533836, 0.0386737104573771, 0.0397794302159846, 0.0352739962796708,
0.0376756204317514, 0.0370614500426065, 0.0374731659969108, 0.035366001926832,
0.0397165124506166, 0.0414814320660011, 0.0431083057931525, 0.0388672853038453,
0.0403590048367136, 0.0461540000449275, 0.0379315295246309, 0.0440664419193363,
0.0404593732981113, 0.0387390924290065, 0.0417832766420881, 0.0409598003097311,
0.0394548129358408)), row.names = c(NA, 30L), class = "data.frame")
R base code
par(mfrow=c(3,3))
col.set=c("green","blue","purple","deeppink","darkorchid","darkmagenta","black","khaki")
loop.vector=1:ncol(mydata)
for(b in loop.vector) {
x.beta<-mydata[,b]
beta <- substr(sub("^beta", '', names(mydata)[b]),1,1)
Cn <- substr(sub("^beta", '',names(mydata)[b]),3,4)
plot(x.beta, type = "n", ylab="", xlab="",
main=bquote(beta[.(beta)]~.(Cn)),
cex.main=1)
mtext("plots of betas",line=-1.5, cex=1, outer = TRUE)
for (k in 1:3){
beta_k=mydata[((nrow(mydata)/3)*k-((nrow(mydata)/3)-1)):
((nrow(mydata)/3)*k),b]
lines(((nrow(mydata)/3)*k-((nrow(mydata)/3)-1)):
((nrow(mydata)/3)*k),beta_k,
col=col.set[k])
legend("topleft", bg="transparent",inset=0.05,legend=paste0("chain_",1:3),
col=col.set, lty=1,box.lty=0, cex=0.8)
}
}
I want the same main title for each plot and one main titile for all plots.
how can I do it using the ggplot2 package?
ggplot2 works best with a long data frame containing variables for x, y, color, etc. This makes a long data frame:
library(tidyverse)
long_data = my_data %>%
mutate(n=1:nrow(my_data), chain=paste0('Chain ', rep(1:3, each=nrow(my_data)/3))) %>%
pivot_longer(cols=c(-n, -chain)) %>%
mutate(name=str_replace(name, '(\\d)_', '[\\1]~~'))
This makes the plot.
ggplot(long_data, aes(n, value, color=chain)) +
geom_line() +
facet_wrap(~name, scales='free_y', ncol=3, dir='v',
labeller=label_parsed) +
scale_color_manual('', values=c('Chain 1'='green', 'Chain 2'='blue', 'Chain 3'='purple')) +
theme_minimal()
Quite similar to #KentJohnson's answer but adding expression labelling of your facets, centered title and using scale_color_manual function to edit color labeling:
library(ggplot2)
library(dplyr)
library(tidyr)
df %>% mutate(Group = rep(c("A","B","C"), each = 10),
Position = 1:30) %>%
pivot_longer(-c(Group,Position), names_to = "Var",values_to = "val") %>%
mutate(Var = factor(Var, levels = c("beta0_C1","beta1_C1","beta2_C1","beta0_C2","beta1_C2","beta2_C2"),
labels = c(expression(beta[0]*"C1"),
expression(beta[1]*"C1"),
expression(beta[2]*"C1"),
expression(beta[0]*"C2"),
expression(beta[1]*"C2"),
expression(beta[2]*"C2")))) %>%
ggplot(aes(x = Position, y = val, color = Group))+
geom_line()+
facet_wrap(.~Var, scales = "free", labeller = label_parsed)+
labs(x = "", y ="", title = "Plots of Betas", color = "")+
scale_color_manual(values = c("green","blue","purple"), labels = c("Chain 1","Chain 2","Chain 3"))+
theme_minimal()+
theme(plot.title = element_text(hjust = 0.5))
For the following data set, I would like to plot for each variable and color each 10th observations differently. I can do it using the R base. I want to learn how to do it using the ggplot2?
dput(mydata)
structure(list(beta0_C1 = c(5.90722120539152, 5.89025566996191,
5.88591520258904, 5.86911167649919, 5.93772460437405, 5.92985640353594,
5.89150365752453, 5.99046628686212, 5.91548006074821, 5.91571832976612,
5.88437484241154, 5.92092513223357, 5.98978050584774, 5.91152552752889,
5.91235823292462, 5.87961960044268, 5.84048698713552, 5.85484766204026,
5.94002829943904, 5.8844367778216, 5.90201348639369, 5.91220967575205,
5.90010933186624, 5.9187781795242, 5.85506764080697, 5.90103565341373,
5.88527143992961, 5.90218851192948, 5.90118162849608, 5.93147588185271
), beta1_C1 = c(0.389473200070741, 0.386495525456602, 0.401277295631578,
0.400952009358693, 0.376727640651344, 0.380365338054745, 0.393444927288697,
0.351041363714069, 0.393194356572458, 0.393448101768608, 0.398884551136789,
0.399458966787235, 0.357436746423815, 0.393782316102096, 0.387154169967002,
0.400838223362088, 0.404272252119662, 0.407427775176583, 0.379704250022161,
0.388842664781329, 0.382202010301184, 0.401354531881688, 0.391184010553641,
0.390280828053183, 0.402135923802544, 0.384344141458216, 0.405409447440106,
0.391719398951194, 0.398025625260563, 0.361822915989445), beta2_C1 = c(-0.0214886993465096,
-0.020723519439664, -0.0224612526333316, -0.0218187226687474,
-0.0200324040063121, -0.0208421378685671, -0.0218756660346625,
-0.0182499666400075, -0.0222765863213226, -0.022242845613047,
-0.0222033291270054, -0.0231570312767931, -0.0189429585905841,
-0.0221017468740293, -0.0209327798783444, -0.022409049257, -0.021698958175968,
-0.0225601087054418, -0.020928341508875, -0.0214668830626075,
-0.0205872002686706, -0.0233768022702472, -0.021755967293395,
-0.0218442145294776, -0.0222514480818199, -0.0212195394692002,
-0.0232109717283908, -0.0214814999754984, -0.0225124468437127,
-0.0187033387452614), beta0_C2 = c(6.50537199380546, 6.43626630601952,
6.44460360859128, 6.44788878017196, 6.49678676895955, 6.48474789770674,
6.5459727637079, 6.37593806532098, 6.39492158034295, 6.44497331914909,
6.3888816168562, 6.49660574813212, 6.45922901141938, 6.40080765767324,
6.37918638201668, 6.49354321098856, 6.47057962920788, 6.55699741431025,
6.56617313133218, 6.54271932949381, 6.44608000042182, 6.45333777656105,
6.67458442747556, 6.48420983182487, 6.59919337271637, 6.46645685814734,
6.46171236062657, 6.52625058117578, 6.51177045919728, 6.49897849935538
), beta1_C2 = c(-0.370455826326915, -0.338852275811034, -0.340671118342601,
-0.339888681238265, -0.36934391822867, -0.357194169746804, -0.415966150286963,
-0.349051278947586, -0.358209379291251, -0.371785518417424, -0.349725822847608,
-0.368220986471866, -0.327425879655177, -0.336993142255552, -0.328859493371605,
-0.347764105375218, -0.329761787134926, -0.37935820670654, -0.400211161919931,
-0.408699321227288, -0.357590345066542, -0.376548827126353, -0.44672514669147,
-0.353840422053319, -0.421912098450693, -0.371491468175642, -0.354864346664247,
-0.39139246919467, -0.379006372881295, -0.372492936183765), beta2_C2 = c(0.039728365796445,
0.0368393936404604, 0.0375019672690036, 0.0375019364609944, 0.0403444583999664,
0.0378627636833333, 0.0446717245407897, 0.0377538641609231, 0.039662572899695,
0.0408055348533836, 0.0386737104573771, 0.0397794302159846, 0.0352739962796708,
0.0376756204317514, 0.0370614500426065, 0.0374731659969108, 0.035366001926832,
0.0397165124506166, 0.0414814320660011, 0.0431083057931525, 0.0388672853038453,
0.0403590048367136, 0.0461540000449275, 0.0379315295246309, 0.0440664419193363,
0.0404593732981113, 0.0387390924290065, 0.0417832766420881, 0.0409598003097311,
0.0394548129358408)), row.names = c(NA, 30L), class = "data.frame")
R base code
par(mfrow=c(3,3))
col.set=c("green","blue","purple","deeppink","darkorchid","darkmagenta","black","khaki")
loop.vector=1:ncol(mydata)
for(b in loop.vector) {
x.beta<-mydata[,b]
beta <- substr(sub("^beta", '', names(mydata)[b]),1,1)
Cn <- substr(sub("^beta", '',names(mydata)[b]),3,4)
plot(x.beta, type = "n", ylab="", xlab="",
main=bquote(beta[.(beta)]~.(Cn)),
cex.main=1)
mtext("plots of betas",line=-1.5, cex=1, outer = TRUE)
for (k in 1:3){
beta_k=mydata[((nrow(mydata)/3)*k-((nrow(mydata)/3)-1)):
((nrow(mydata)/3)*k),b]
lines(((nrow(mydata)/3)*k-((nrow(mydata)/3)-1)):
((nrow(mydata)/3)*k),beta_k,
col=col.set[k])
legend("topleft", bg="transparent",inset=0.05,legend=paste0("chain_",1:3),
col=col.set, lty=1,box.lty=0, cex=0.8)
}
}
I want the same main title for each plot and one main titile for all plots.
how can I do it using the ggplot2 package?
ggplot2 works best with a long data frame containing variables for x, y, color, etc. This makes a long data frame:
library(tidyverse)
long_data = my_data %>%
mutate(n=1:nrow(my_data), chain=paste0('Chain ', rep(1:3, each=nrow(my_data)/3))) %>%
pivot_longer(cols=c(-n, -chain)) %>%
mutate(name=str_replace(name, '(\\d)_', '[\\1]~~'))
This makes the plot.
ggplot(long_data, aes(n, value, color=chain)) +
geom_line() +
facet_wrap(~name, scales='free_y', ncol=3, dir='v',
labeller=label_parsed) +
scale_color_manual('', values=c('Chain 1'='green', 'Chain 2'='blue', 'Chain 3'='purple')) +
theme_minimal()
Quite similar to #KentJohnson's answer but adding expression labelling of your facets, centered title and using scale_color_manual function to edit color labeling:
library(ggplot2)
library(dplyr)
library(tidyr)
df %>% mutate(Group = rep(c("A","B","C"), each = 10),
Position = 1:30) %>%
pivot_longer(-c(Group,Position), names_to = "Var",values_to = "val") %>%
mutate(Var = factor(Var, levels = c("beta0_C1","beta1_C1","beta2_C1","beta0_C2","beta1_C2","beta2_C2"),
labels = c(expression(beta[0]*"C1"),
expression(beta[1]*"C1"),
expression(beta[2]*"C1"),
expression(beta[0]*"C2"),
expression(beta[1]*"C2"),
expression(beta[2]*"C2")))) %>%
ggplot(aes(x = Position, y = val, color = Group))+
geom_line()+
facet_wrap(.~Var, scales = "free", labeller = label_parsed)+
labs(x = "", y ="", title = "Plots of Betas", color = "")+
scale_color_manual(values = c("green","blue","purple"), labels = c("Chain 1","Chain 2","Chain 3"))+
theme_minimal()+
theme(plot.title = element_text(hjust = 0.5))
For some reason, I have to make a plot that looks more or less like this:
For this I use the following code:
library(ggplot2)
library(tidyverse)
set.seed(10)
df<-data.frame(Meas = runif(1000,0,10),
Prop1 = sample(x = LETTERS[1:3],1000,replace=TRUE),
Prop2 = sample(x = letters[1:5],1000,replace=TRUE),
Prop3 = sample(x=c("monkey","donkey","flipper"),1000,replace=TRUE))%>%
gather(Prop,Propvalue,-Meas)
ggplot(df,aes(x = Propvalue,y=Meas))+
geom_boxplot()+
facet_wrap(~Prop,ncol=2,scales="free_y")+
coord_flip()
I believe this would look better if the y-ticks on the right graph would appear on the right (for the graphs on the left, the y-ticks should remain where they are, but flipper and donkey should appear on the right side to avoid the gap between the left and right panels), but I can't find a way to do this.
Here's a hack that utilises ggplot's sec.axis argument, which creates a secondary axis opposite the primary axis & has to be a one-to-one mapping of it. I call this a hack, because this works only for continuous axis, so we need to map the categorical Propvalue to numeric values.
Note: I assumed in this example that you want all odd numbered PropX facets' labels on the left, & even numbered PropX facets' labels on the right. You can also tweak the options for other variations.
library(ggplot2)
library(tidyverse)
# generate data
set.seed(10)
df<-data.frame(Meas = runif(1000,0,10),
Prop1 = sample(x = LETTERS[1:3],1000,replace=TRUE),
Prop2 = sample(x=c("monkey","donkey","flipper"),1000,replace=TRUE),
Prop3 = sample(x = letters[1:5],1000,replace=TRUE))%>%
gather(Prop,Propvalue,-Meas)
# map Propvalue to integers, primary axis contents, & secondary axis contents.
df2 <- df %>%
mutate(Propvalue.int = as.integer(factor(Propvalue,
levels = df %>% select(Prop, Propvalue) %>%
arrange(Prop, Propvalue) %>% unique() %>%
select(Propvalue) %>% unlist())),
facet.column = ifelse(as.integer(str_extract(Prop, "[0-9]")) %% 2 == 0, 2, 1),
Propvalue.left = ifelse(facet.column == 1, Propvalue, ""),
Propvalue.right = ifelse(facet.column == 2, Propvalue, ""))
# create mapping table
integer2factor <- df2 %>%
select(Propvalue.int, Propvalue.left, Propvalue.right) %>%
unique() %>% arrange(Propvalue.int)
ggplot(df2,aes(x = Propvalue.int, y=Meas,
group = Propvalue.int))+
geom_boxplot() +
scale_x_continuous(breaks = integer2factor$Propvalue.int,
labels = integer2factor$Propvalue.left,
name = "Propvalue",
sec.axis = dup_axis(breaks = integer2factor$Propvalue.int,
labels = integer2factor$Propvalue.right,
name = "")) +
facet_wrap(~Prop,ncol=2,scales="free")+
coord_flip() +
theme(axis.ticks.y = element_blank())
I believe this will do the trick.
library(ggplot2)
library(tidyverse)
library(tidyr)
set.seed(10)
df <-data.frame(Meas = runif(1000,0,10),
Prop1 = sample(x = LETTERS[1:3],1000,replace=TRUE),
Prop2 = sample(x = letters[1:5],1000,replace=TRUE),
Prop3 = sample(x=c("monkey","donkey","flipper"),1000,replace=TRUE))%>%
gather(Prop,Propvalue,-Meas)
ggplot(df,aes(x = Propvalue,y=Meas))+
geom_boxplot()+
facet_wrap(~Prop,ncol=2,scales="free_y")+
coord_flip()
p.list = lapply(sort(unique(df$Prop)), function(i) { # i <- "Prop1"
ggplot(df[df$Prop==i,],aes(x = Propvalue, y=Meas))+
geom_boxplot()+
facet_wrap(~Prop,scales="free_y")+
coord_flip()
})
p.list[[2]] <- p.list[[2]] + scale_x_discrete(position = "top")
library(gridExtra)
do.call(grid.arrange, c(p.list, nrow=2))
# I am trying to combine a horizontal beside barplot with the table
# with the values in it.
# E.g. original table, including sample_ids
df = data.frame(
sample_id=c("s01","s02","s03","s04","s05","s06","s07","s08","s09","s10"),
one=runif(10,0,10),
two=runif(10,0,10),
three=runif(10,0,10),
four=runif(10,0,10)
)
# I created a mydata that I then do barplot as matrix
mydata = data.frame(
one=df$one,
two=df$two,
three=df$three,
four=df$four
)
# Plotted, using rainbow colouring, with a legend in the top right
barplot(as.matrix(mydata),horiz=TRUE,beside=TRUE,col=rainbow(length(df$sample_id)), legend=paste(df$sample_id), args.legend = list(x = "topright", bty = "n"),xlim=c(0,20))
# Now I would like the grid.table to be on the bottom right, ideally with the same order and colouring as the legend
library(gridExtra)
grid.table(df)
# Any ideas?
# EDIT: also tried addtable2plot from plotrix, with no much success
bp = barplot(as.matrix(mydata),horiz=TRUE,beside=TRUE,col=rainbow(length(df$sample_id)), legend=paste(df$sample_id), args.legend = list(x = "topright", bty = "n"),xlim=c(0,20))
library(plotrix)
addtable2plot(bp, y=0, df,cex=0.3)
The other option would be to turn the barplot into a ggplot geom_bar, but I struggled to do it for more than 2 columns.
Here's one way to do it using addtable2plot of plotrix package. It allows you to use the legend positions such as "bottomright"
df = data.frame(
sample_id=c("s01","s02","s03","s04","s05","s06","s07","s08","s09","s10"),
one=runif(10,0,10),
two=runif(10,0,10),
three=runif(10,0,10),
four=runif(10,0,10)
)
mydata = data.frame(
one=df$one,
two=df$two,
three=df$three,
four=df$four
)
library(plotrix)
dev.off()
windows(width = 8, height = 6)
df$one = round(df$one,2)
df$two = round(df$two,2)
df$three = round(df$three,2)
df$four = round(df$four,2)
barplot(as.matrix(mydata),horiz=TRUE,beside=TRUE,col=rainbow(length(df$sample_id)),
legend=paste(df$sample_id),
args.legend = list(x = "topright", bty = "n", cex = 1),
xlim=c(0,20))
addtable2plot("bottomright",table = df, cex = .9, bty = "o",
bg = c("white","grey"), vlines = TRUE, xpad = .25)
If you want to make the barplot in ggplot2, you need to reshape your data into long format. Based on your example data, the following code:
library(ggplot2)
library(gridExtra)
library(reshape2)
bp <- ggplot(melt(df, id.vars = 1),
aes(x = variable, y = value, fill = sample_id)) +
geom_bar(stat = 'identity', position = 'dodge') +
scale_fill_manual(values = rainbow(10)) +
labs(x = NULL, y = NULL) +
coord_flip() +
theme_minimal(base_size = 14)
gt <- tableGrob(df, rows = NULL, theme = ttheme_minimal())
grid.arrange(bp, gt, ncol = 2, widths = c(2.5,2))
which gives the following result: