Plot multiple datasets with ggplot
Here's a template for plotting two data frame in the same figure:
A = data.frame(x = rnorm(10),y=rnorm(10))
B = data.frame(x = rnorm(10),y=rnorm(10))
ggplot(A,aes(x,y)) +geom_point() +geom_point(data=B,colour='red') + xlim(0, 10)
or equivalently:
qplot(x,y,data=A) +geom_point(data=B,colour='red') + xlim(0, 10)
If you want to plot to figures side by side, see ?par
and look for the descriptions of 'mfcol' and 'mfrow'
In addition to ggsave, see ?pdf
.
How to plot plots using different datasets using ggplot2
Given the careful and reproducible way you created your question I am not just referring to the old answer as it may be harder to transfer the subsetting etc.
You initialize a new ggplot
object whenever you run ggplot(...)
.
If you want to add a layer on top of an existing plot you have to operate on the same object, something like this:
ggplot(data=df, aes(x=Credible, y=len, group=1)) +
geom_line(color="red")+
geom_point()+
labs(x = "Credible", y = "") +
geom_point(data=zero, color="green", aes(x=x0, y=y0, group=1))
Note how in the second geom_point
the data source and aesthetics are explicitly specified instead to prevent them being inherited from the initial object.
Plotting multiple grouped variable datasets in ggplot
You could try this
library(ggplot2)
library(dplyr)
df_melted <- bind_rows(df.1.melted, df.2.melted)
df_melted %>%
mutate(df = rep(c('df.1', 'df.2'), each = nrow(df_melted) / 2)) %>%
ggplot(aes(x = variable,
y = value,
col = df)) +
geom_line(aes(group = name)) +
geom_point() +
scale_y_log10(limits = c( 1, 10),
breaks = c(1, 10)) +
scale_color_manual(values = c('df.1' = "forestgreen",
'df.2' = "orange"))
The idea is to create one data frame, df_melted
, and add the column df
that indicates from which data frame the observations came from. Then you can map variable df
to the colour aesthetic. As suggested in the comment you can change the default colours using scale_colour_manual
.
How can I create a loop in R for plotting multiple datasets in sequence?
for (MyFile in list.files()){
print(MyFile)
## do other stuff
}
One ggplot with multiple data sets
A solution is described here and here.
Install ggnewscale
and then add a new scale using new_scale
:
library(ggplot2)
library(ggnewscale)
dta1 = data.frame(storename = c(letters[1:5]),
storesize = c("small","small","medium","large","large"),
lon = c(10,11.2,15,19,22),
lat = c(5,5.8,5.6,6.1,3.4))
dta2 = data.frame(population = sample(100,100,replace = T),
lon = runif(100,10,22),
lat = runif(100,3.5,6))
ggplot() +
geom_point(data=dta1, aes(x=lon, y=lat,size=storesize), shape=23, fill="blue") +
scale_size_manual(values = c(1,2,3)) +
new_scale("size") +
geom_point(data=dta2, aes(x=lon, y=lat, size=population), shape=21, fill="orange")
plot multiple datasets and compare categories with barplots
Here's code that may help you reach your goal. Note that I took some liberties with your input data because it seems to be incomplete in your question.
library(ggplot2)
List_before <- data.frame(
list_id = "list_before",
name = c("Tracker1", "Tracker2", "Tracker3", "Tracker4","Tracker5","Tracker6"),
number_trackers = sample(c(1, 2, 3, 4, 5, 6)),
category = c("Ads", "Analytics", "Other 1", "Other 2", "Other 3", "Other 4"),
c4 = c("url1.com","ur2.com","url3.com","url4.com","url5.com","url6.com"))
List_short_after <- data.frame(
list_id = "list_short_after",
name = c("Tracker1", "Tracker2", "Tracker3", "Tracker4","Tracker5","Tracker6"),
number_trackers = sample(c(1, 2, 3, 4, 5, 6)),
category = c("Ads", "Analytics", "Other 1", "Other 2", "Other 3", "Other 4"),
c4 = c("url1.com","ur2.com","url3.com","url4.com","url5.com","url6.com"))
List_after <- data.frame(
list_id = "list_after",
name = c("Tracker1", "Tracker2", "Tracker3", "Tracker4","Tracker5","Tracker6"),
number_trackers = sample(c(1, 2, 3, 4, 5, 6)),
category = c("Ads", "Analytics", "Other 1", "Other 2", "Other 3", "Other 4"),
c4 = c("url1.com","ur2.com","url3.com","url4.com","url5.com","url6.com"))
df <- rbind(List_before, List_short_after, List_after)
df$list_id <- as.factor(df$list_id)
df$category <- as.factor(df$category)
ggplot(df, aes(y = number_trackers, x = list_id)) +
geom_bar(aes(fill = list_id), stat = "identity", position = position_dodge()) +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
facet_grid(~category) +
labs(y = "Number of Trackers", x = NULL)
plot boxplots from two datasets side by side
This type of problems generally has to do with reshaping the data. The format should be the long format and the data is in wide format. See this post on how to reshape the data from wide to long format.
But fore reshaping the data, create a new column telling from which data set is this data and bind the two data sets.
x <- '
ID score1 score2
1 200 300
2 300 150
3 400 -100'
y <- '
ID score1 score2
200 200 300
300 300 150
400 400 -100'
df1 <- read.table(textConnection(x), header = TRUE)
df2 <- read.table(textConnection(y), header = TRUE)
dfboth <- rbind(
cbind(data = 1, df1),
cbind(data = 2, df2)
)
suppressPackageStartupMessages({
library(dplyr)
library(tidyr)
library(ggplot2)
})
bind_rows(
df1 %>% mutate(data = "1"),
df2 %>% mutate(data = "2")
) %>%
pivot_longer(starts_with("score"), names_to = "score") %>%
ggplot(aes(data, value, fill = score)) +
geom_boxplot() +
xlab("Data set")
Created on 2022-04-29 by the reprex package (v2.0.1)
Related Topics
Create a Dataframe with Random Numbers in Each Column
Freezing Header and First Column Using Data.Table in Shiny
Simple R 3D Interpolation/Surface Plot
Load a Dataset into R with Data() Using a Variable Instead of the Dataset Name
Loop for Reverse Geocoding in R
Flatten Nested List into 1-Deep List
Doing T.Test for Columns for Each Row in Data Set
Behavior of Summing !Is.Na() Results
How to Colour the Labels of a Dendrogram by an Additional Factor Variable in R
To Display Two Heatmaps in Same PDF Side by Side in R
How to Make Single Stacked Bar Chart in Ggplot2
How to Access the Name of the Variable Assigned to the Result of a Function Within the Function
R - Scaling Numeric Values Only in a Dataframe with Mixed Types
How to Display Strip Labels Below the Plot When Faceting
R Ggplot2 Boxplots - Ggpubr Stat_Compare_Means Not Working Properly