R- Connecting the maximum and the minimum value of two adjacent categorical variables in ggplot2 -
i have scatter plot now. each color represent categorical group , each group has range of values on x-axis. there should not overlapping between range of categorical variables. however, because of thickness of scatter points, looks there overlapping. so, want draw line connect maximum point of group , minimum point of adjacent group long line not have negative slope, can show there no overlapping between each categorical variable.
i not know how use geom_line() connect 2 points y-coordinate categorical variable. possible so??
any appreciated!!!
it sounds want geom_segment
not geom_line
. you'll need aggregate data new data frame has points want plotted. adapted brian's sample data , use dplyr
this:
# sample data df <- data.frame(xvals = runif(50, 0, 1)) df$cats <- cut(df$xvals, c(0, .25, .625, 1)) # aggregation library(dplyr) df_summ = df %>% group_by(cats) %>% summarize(min = min(xvals), max = max(xvals)) %>% mutate(adj_max = lead(max), adj_min = lead(min), adj_cat = lead(cats)) # plot ggplot(df, aes(xvals, cats, colour = cats)) + geom_point() + geom_segment(data = df_summ, aes( x = max, xend = adj_min, y = cats, yend = adj_cat ))
you can keep segments colored previous category, or maybe set them neutral color don't stand out much.
Comments
Post a Comment