scatter plot - Add pair lines in R -
i have data measured pair-wise (e.g. 1c, 1m, 2c , 2m), have plotted separately (as c , m). however, add line between each pair (e.g. line point 1 in c column point 1 in m 'column').
a small section of entire dataset:
pairnumber type m 1 m 0.117133 2 m 0.054298837 3 m 0.039734 4 m 0.069247069 5 m 0.043053957 1 c 0.051086898 2 c 0.075519 3 c 0.065834198 4 c 0.084632915 5 c 0.054254946
i have generated below picture using following tiny r snippet:
boxplot(test$m ~ test$type) stripchart(test$m ~ test$type, vertical = true, method="jitter", add = true, col = 'blue')
i know command or function need achieve (a rough sketch of desired result, of lines, presented below).
alternatively, doing ggplot fine me, have following alternative ggplot code produce plot similar first 1 above:
ggplot(,aes(x=test$type, y=test$m)) + geom_boxplot(outlier.shape=na) + geom_jitter(position=position_jitter(width=.1, height=0))
i have been trying geom_path
, have not found correct syntax achieve want.
i recommend breaking multiple visualizations -- more data, feel type of plot become difficult interpret. in addition, not sure it's possible draw geom_lines
, connect them additional call geom_jitter
. being said, gets of way there:
ggplot(df, aes(x = type, y = m)) + geom_boxplot(outlier.shape = na) + geom_line(aes(group = pairnumber)) + geom_point()
the trick specify group
aesthetic within geom_line()
, not top within ggplot()
.
additional note: no reason qualify aesthetic variables within ggplot()
-- is, no reason ggplot(data = test, aes(x = test$type, y = test$m)
; rather, use: ggplot(data = test, aes(x = type, y = m))
.
update
leveraging cowplot
visualize data in different plots prove helpful:
library(cowplot) p1 <- ggplot(df, aes(x = type, y = m, color = type)) + geom_boxplot() p2 <- ggplot(df, aes(x = type, y = m, color = type)) + geom_jitter(position = position_jitter(width = 0.1, height = 0)) p3 <- ggplot(df, aes(x = m, color = type, fill = type)) + geom_density(alpha = 0.5) p4 <- ggplot(df, aes(x = type, y = m)) + geom_line(aes(group = pairnumber, color = factor(pairnumber))) plot_grid(p1, p2, p3, p4, labels = c(letters[1:4]), align = "v")
Comments
Post a Comment