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,130 points
222 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 darklord
• 6,170 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
1,721 views
0 votes
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,020 points
3,306 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,130 points
190 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
39 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 4, 2018 in Data Analytics by Kalgi
• 42,330 points
60 views
0 votes
1 answer
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
159 views
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 darklord
• 6,170 points
170 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 in Data Analytics by anonymous
1,719 views