4
votes

I have 2 datasets with unequal lengths for plotting using ggplots2:

Data A;

column x column y
0.23     1.54    
0.44     1.46
0.69     1.37
0.70     1.21
0.75     1.01
0.88     0.91 

Data B:

column x column y
0.13     1.24    
0.34     1.16
0.49     1.07
0.54     0.99
0.69     1.01

I'm sure of how to write a code in ggplot2 for plotting these two data sets together. In both cases, plots shown as x axis = column x and y axis= column y. Can someone help me please?

James

3
Fantastic!!! Thank you Chase and user2194122, I got the two codes to work. But, I'm left with two problems still. I will like to be able to control the point color and size(example one dataset points with big blue and the other with small red), and how to suppress the column name read from the data and instead display longer names on x and y axes.James Johnson

3 Answers

5
votes

Suppose you have datasets A and B as a data.frame:

A <- data.frame(x=1:5, y=11:15)
B <- data.frame(x=1:10, y=20:11)

You have to join them together:

df <- rbind(A, B) # Join A and B together.
df
    x  y
1   1 11
2   2 12
3   3 13
4   4 14
5   5 15
6   1 20
7   2 19
8   3 18
9   4 17
10  5 16
11  6 15
12  7 14
13  8 13
14  9 12
15 10 11

Then you can plot it:

ggplot(data=df, aes(x=x, y=y)) + geom_point()

If you want to distinguish points from dataset A and B by color:

df$dataset <- c(rep("A", nrow(A)), rep("B", nrow(B)))
df
    x  y dataset
1   1 11       A
2   2 12       A
3   3 13       A
4   4 14       A
5   5 15       A
6   1 20       B
7   2 19       B
8   3 18       B
9   4 17       B
10  5 16       B
11  6 15       B
12  7 14       B
13  8 13       B
14  9 12       B
15 10 11       B

ggplot(data=df, aes(x=x, y=y, col=dataset)) + geom_point()

If you want to distinguish points from dataset A and B by color and size and change axis labels:

ggplot(data=df, aes(x=x, y=y, col=dataset, size=dataset)) + geom_point() +
scale_color_manual(name="Dataset", labels = c("Data A","Data B"), values=c("red", "blue")) + 
scale_size_manual(name="Dataset", labels = c("Data A","Data B"), values=c(10, 5)) + 
xlab("xxxx") + ylab("yyyy")

See Tutorial or use google :).

2
votes

I know this comes up all the time when looking to plot data points (which are sparse) and a a line from a theoretical curve (which has lots of data points)

In this case you can give the different aesthetic mappings to each piece of ggplot's geometries, individually.

E.g. [EDITED HERE TO MAKE THE BEST EXAMPLE BE FIRST]

ggplot() +
  geom_point(data = df_A, aes(x, y)) + 
  geom_line(data = df_B, aes(x, y), color = "red") +
  theme_minimal() 

or

ggplot() +
  with(df_A, geom_point(aes(x, y))) + 
  with(df_B, geom_line(aes(x, y)), color = "red") +
  theme_minimal() 
1
votes

One option is to get the data into one data.frame. Here's an example using ldply() from plyr(), assuming your data.frames are named d1 and d2:

library(plyr)
> d3 <- ldply(list(d1 = d2, d2 = d2))
> rbind(head(d3,2), tail(d3,2))
   .id column.x column.y
1   d1     0.13     1.24
2   d1     0.34     1.16
9   d2     0.54     0.99
10  d2     0.69     1.01

Or simply pass the different datasets to different geoms when plotting. Something like this:

ggplot() +
  geom_point(data = d1, aes(column.x, column.y)) +
  geom_point(data = d2, aes(column.x, column.y), colour = "red")