I have a piece of code that is used to generate multiple plots from a dataset. The dataset is filtered based on the parameters required of the visualization and is plotted using ggplot.
library(ggplot2)
summary <- filter(dataframe)
plot <- ggplot(summary)
Now, I have multiple chunks of code for filtering each type of graph and each chunk has a 'summary' and 'plot'.
Is there a way I can plot multiple functions called 'plot' simultaneously? And is there a way to create a userinterface/button that when clicked will generate all the plots in one go?
If each plot is the same type of plot but with different data you could write it once an as #Victor Ordu suggests put them in a list using a loop or something like lapply or map() in the purrr package.
You could then arrange them using ggarrange from the ggpubr package.
For example:
library(tidyverse)
library(ggpubr)
# Plot function
example_plot <- function(x){
ggplot(x, aes(drat, wt)) +
geom_point()
}
# Summarise in a dataframe and plot into a list
graphs <- mtcars %>%
group_by(cyl) %>%
nest() %>%
mutate(graph = map(data, ~ example_plot(.x))) %>%
pull(graph)
# Plot them all!
ggarrange(graphs[[1]], graphs[[2]], graphs[[3]], ncol = 3)
Otherwise just skip straight to arranging them.
Related
I'm trying to plot 2,695 different plots using the columns of my dataset. The x axis will be constant for all the datasets which is the "instrument.supersaturation" column. As for the y axis it will be the remaining columns label with date and times.
I have tried the following code to plot all 2,695 plots using the loop function. The code works and it shows the x-axis points as the instrument supersaturation values, but I'm having trouble plotting the y-axis using the concentrations of my column so it give a straight line on the plot.
library(ggplot2)
col_names <- colnames(rotated.plot.data)
col_names <- col_names[-1]
for(i in col_names){
plot <- ggplot(rotated.plot.data, aes(x=rotated.plot.data$instrument.supersaturation, y="i"))+
geom_point()
print(plot)}
Tried it in your way. The error arises from - i in inverted commas as ggplot does not recognize it. sym function removes inverted commas and eval function will evaluate it as an expression.
Phils method would be much easier if you are familiar with map()
library(ggplot2)
library(tidyverse)
iris<-iris %>% select(-c(Species))
for(i in 1:(length(colnames(iris))-1)){
plot <- ggplot(iris, aes(x=Sepal.Length, y=eval(sym(colnames((iris[i+1]))))))+
geom_point()
print(plot)}
I have used the following code
ggplot(IncomeGroup_count,
aes(x=Income_Group,
y= Percentage,
colour = 'lightblue'))
But the only thing it produces is the x and y axis with no bars.
It looks like you are missing the geom_bar function. Typically ggplot works as the base function for fitting whatever data you are using and then the geom functions "draw" the plot using that data. Here I have constructed a bar plot using data in R since you have not supplied your own data in your question.
#### Load Library ####
library(tidyverse)
#### Plot ####
airquality %>%
ggplot(aes(x=is.na(Ozone)))+
geom_bar()
Which gives you this bare bones bar plot. It takes the NA values from the airquality dataset, then draws bars thereafter plotting the NA values by "TRUE" for NA and "FALSE" for not NA:
Edit
Again it's difficult to guess what is going wrong if you don't share your data, so I'm assuming based off your comments below that you are trying to plot y explicitly as a count. As suggested in the comments, geom_col may be better for this. Using a different example from the same dataset:
airquality %>%
group_by(Month) %>%
summarise(Mean_Ozone = mean(Ozone, na.rm=T)) %>%
ggplot(aes(x=Month,
y=Mean_Ozone))+
geom_col()
You get this:
Using the iris dataset..
Sample code and function:
plotfunction <- function(whatspecies){
baz <- iris %>% filter(Species == whatspecies) %>%
ggplot(aes(Petal.Width, Petal.Length)) +
geom_point() +
labs(title = whatspecies)
ggsave(filename = paste0(whatspecies,".png"),
path = getwd())
return(baz)
}
What I'd like to do is to loop over the Species variable to create 3 plots in my working directory. In my real data frame I have many more factors so I was wondering if there is a better way to do this rather than running the function n number of times - as in this instance I only care about modifying/looping over one variable in each graph.
Edit: In my circumstance I require independent plots so I can't use facets or different aesthetics.
Is this what you are looking for?
library(dplyr)
library(ggplot2)
for (sp in levels(iris[["Species"]])) {
plotfunction(sp)
}
I have a two set of data with different length.
Sameple datatype is:
A=c(423,430,500,460,457,300,325,498,450,453,486,459)
B=c(300,325,356345,378,391,367)
I want to create boxplot for them within a same graph and same scale. I tried it in ggplot2 in R. I also tried default boxplot in R.
boxplot (A~B)
but it showed error. I would like to use ggplot2 in R.
You have to create a dataset with those 2 vectors and then plot.
library(ggplot2)
A=c(423,430,500,460,457,300,325,498,450,453,486,459)
B=c(300,325,356345,378,391,367)
# create a dataset for each vector
df_A = data.frame(value=A, id="A")
df_B = data.frame(value=B, id="B")
# combine datasets
df = rbind(df_A, df_B)
# create the box plot
ggplot(df, aes(id, value)) + geom_boxplot()
I would like to plot both a histogram to a fitted Weibull function on the same graph. The code to plot the histogram is:
hist(data$grddia2, prob=TRUE,breaks=5)
The code for the fitted Weibull function is:(Need the MASS package)
fitdistr(data$grddia2,densfun=dweibull,start=list(scale=1,shape=2))
How do I plot both together on the same graph. I've attached the data set.
Also, bonus to anyone who can provide code that can achieve the same thing, but create a graph for each column of data. Many columns within a data set. Would be nice to have all graphs on the same page.
https://www.dropbox.com/s/ra9c2kkk49vyyyc/Diameter%20Distribution.csv?dl=0
Here is the code
library("ggplot2")
library("dplyr")
library("tidyr")
library("MASS")
# Import dataset and filter the column "treeno"
# Use namespace dplyr:: explicitly because of conflict with MASS:: for function "select"
data <- read.csv("Diameter Distribution.csv") %>%
dplyr::select(-treeno)
# Function to provide the Weibull distribution for each column
# The distribution is calculated based on the estimated scale and shape parameters of the input
fitweibull <- function(column) {
x <- seq(0,7,by=0.01)
fitparam <- column %>%
unlist %>%
fitdistr(densfun=dweibull,start=list(scale=1,shape=2))
return(dweibull(x, scale=fitparam$estimate[1], shape=fitparam$estimate[2]))
}
# Apply function for each column then consolidate all in a data.frame
fitdata <-data %>%
apply(2, as.list) %>%
lapply(FUN = fitweibull) %>%
data.frame()
# Display graphs
multiplyingFactor<-10
ggplot() +
geom_histogram(data=gather(data), aes(x=value, group=key, fill=key), alpha=0.2) +
geom_line(data=gather(fitdata), aes(x=rep(seq(0,7,by=0.01),ncol(fitdata)), y=multiplyingFactor*value, group=key, color=key))
And the output figure
Variant: thanks to the wonderful ggplot2 package you can also have the graphs apart just by adding this final line of code
+ facet_wrap(~ key) + theme(legend.position = "none")
Which gives you this other figure: