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
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 :).
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With