How to stack scatterplot in ggplot2

0 votes

I am trying to create scatterplots in R using ggplot2 to visualize populations over time. 

My data set looks like the following:

sampling_period     cage     total_1    total_2     total_3
              4        y          34         95          12
              4        n          89         12          13
              5        n          23         10           2

I have been making individual scatterplots for the variables total_1, total_2, and total_3 with the following code:

qplot(data=BLPcaged, x=sampling_period, y=total_1, color=cage_or_control)
qplot(data=BLPcaged, x=sampling_period, y=total_2, color=cage_or_control)
qplot(data=BLPcaged, x=sampling_period, y=total_3, color=cage_or_control)

How to create a scatterplot that contains the information about all three populations over time?

I want the final product to be composed of three scatterplots one on top of each other and have the same scale for the axes. 

I know that I can use facet to make different plots for the levels of a factor, but can it also be used to create different plots for different variables?

May 8, 2018 in Data Analytics by DataKing99
• 8,250 points
2,524 views

1 answer to this question.

0 votes

Use melt() to reshape your data with total as a factor that you can facet on:

BLPcaged = data.frame(sampling_period=c(4,4,5),
                      cage=c('y','n','n'),
                      total_1=c(34,89,23),
                      total_2=c(95,12,10),
                      total_3=c(12,13,2))

library(reshape2)
BLPcaged.melted = melt(BLPcaged,
                       id.vars=c('sampling_period','cage'),
                       variable.name='total')

So now BLPcaged.melted looks like this:

  sampling_period cage   total value
1               4    y total_1    34
2               4    n total_1    89
3               5    n total_1    23
4               4    y total_2    95
5               4    n total_2    12
6               5    n total_2    10
7               4    y total_3    12
8               4    n total_3    13
9               5    n total_3     2

You can then facet this by total:

ggplot(BLPcaged.melted, aes(sampling_period, value, color=cage)) + 
geom_point() + 
facet_grid(total~.)
answered May 8, 2018 by Sahiti
• 6,370 points

Related Questions In Data Analytics

0 votes
1 answer

How to plot side-by-side Plots with ggplot2 in R?

By Using gridExtra library we can easily ...READ MORE

answered Apr 16, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by MD 8,876 views
+1 vote
1 answer

How to change fill color in each facet using ggplot2?

You can map the facetting variable to ...READ MORE

answered May 8, 2018 in Data Analytics by kappa3010
• 2,090 points
25,524 views
0 votes
1 answer

How to order bars in a bar graph using ggplot2?

The key to ordering is to set ...READ MORE

answered Jun 1, 2018 in Data Analytics by DataKing99
• 8,250 points
1,211 views
0 votes
1 answer

How to create the Scatterplot in R?

Hi@akhtar, Scatterplots show many points plotted in the ...READ MORE

answered Oct 30, 2020 in Data Analytics by MD
• 95,460 points
690 views
0 votes
1 answer
0 votes
1 answer

How to change mulitiple characters in a column to a date

Firstly we have to set dataf variable ...READ MORE

answered Apr 3, 2018 in Data Analytics by DeepCoder786
• 1,720 points
802 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 5, 2018 in Data Analytics by Kalgi
• 52,350 points
1,136 views
0 votes
1 answer
0 votes
1 answer

How to change y axis max in time series using R?

The axis limits are being set using ...READ MORE

answered Apr 3, 2018 in Data Analytics by Sahiti
• 6,370 points
3,853 views
0 votes
2 answers

How to count unique values in R?

You can try this way, as.data.frame(v) %>% count(v) READ MORE

answered Aug 8, 2019 in Data Analytics by anonymous
6,639 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP