Create a line plot using categorical data and not connecting the lines

我是研究僧i 提交于 2020-03-19 06:12:29

问题


Trying to create a graph where both x and y are factors but I don't want the lines to be connected if there is a gap. How can I achieve this?

library(ggplot2)

df <- data.frame(x = c('a', 'b', 'c', 'd', 'e'), y = c('a', 'a', NA, 'a', 'a'))

ggplot(df, aes(x = x, y = y, group = y)) +
  geom_point() + 
  geom_line()

Dont want the NA in the plot and there shouldn't be a line between b and d.


回答1:


This may need extra work with your full dataset but one approach is to create a grouping variable to use in ggplot to prevent connections that aren't wanted.

df <- data.frame(x = c('a', 'b', 'c', 'd', 'e'), y = c('a', 'a', NA, 'a', 'a'), stringsAsFactors = FALSE)

df %>% 
  mutate(grp = with(rle(y), rep(seq_along(lengths), lengths))) %>%  # y can't be a factor
  mutate_all(as.factor) %>%
  na.omit() %>%                              # Drop NA cases so they're not plotted
  ggplot(aes(x = x, y = y, group = grp)) +
  geom_point() + 
  geom_line() +
  scale_x_discrete(drop = FALSE)             # Preserve empty factor levels in the plot




回答2:


Another way is to factorise y and use the levels. Group with group = 1. You can relabel with scale.

library(ggplot2)
df <- data.frame(x = c('a', 'b', 'c', 'd', 'e'), 
                 y = c('a', 'a', NA, 'a', 'a'))

ggplot(df, aes(x = x, y = as.numeric(as.factor(y)), group = 1)) +
  geom_point() + 
  geom_line() +
  scale_y_continuous(breaks = 1, labels = 'a') +
  labs(y = 'y')
#> Warning: Removed 1 rows containing missing values (geom_point).

Created on 2020-03-04 by the reprex package (v0.3.0)



来源:https://stackoverflow.com/questions/60505478/create-a-line-plot-using-categorical-data-and-not-connecting-the-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!