ggplot中,geom_jitter()和position_jitter()都是处理点的处理图像重叠的问题。
如图,
## Error in `library()`:
## ! there is no package called 'tidyverse'
## Error in `library()`:
## ! there is no package called 'lubridate'
data1 <- tibble::tribble(
~logtype, ~inserttime,
"APP主登录", "2017-07-29 13:10:31",
"延伸登录", "2017-08-29 19:03:12",
"延伸登录", "2017-08-29 19:07:17",
"独立登录", "2017-08-31 17:28:07",
"独立登录", "2017-08-31 17:28:07",
"独立登录", "2017-08-31 17:45:24",
"独立登录", "2017-09-05 02:31:37",
"独立登录", "2017-09-05 02:32:15",
"独立登录", "2017-09-05 02:34:50",
"M站主登录", "2017-09-05 02:52:02"
) %>%
rename(item=logtype, start = inserttime) %>%
mutate(start = ymd_hms(start))## Error in `tibble::tribble(~logtype, ~inserttime, "APP主登录", "2017-07-29 13:10:31", "延伸登录",
## "2017-08-29 19:03:12", "延伸登录", "2017-08-29 19:07:17", "独立登录",
## "2017-08-31 17:28:07", "独立登录", "2017-08-31 17:28:07", "独立登录",
## "2017-08-31 17:45:24", "独立登录", "2017-09-05 02:31:37", "独立登录",
## "2017-09-05 02:32:15", "独立登录", "2017-09-05 02:34:50", "M站主登录",
## "2017-09-05 02:52:02") %>% rename(item = logtype, start = inserttime) %>%
## mutate(start = ymd_hms(start))`:
## ! could not find function "%>%"
data1 %>%
# mutate(start = as.Date(start)) %>%
ggplot(aes(x = start, y = fct_reorder(item, start, .desc = TRUE))
) +
geom_point(
# position=position_jitter(width=0.5,height=0),
size = 6,
alpha = 0.5,
col = 'gold'
) +
xlab(NULL) +
ylab(NULL) +
theme_light() +
theme(
legend.position = "none",
panel.border = element_blank(),
) +
theme(text = element_text(family = 'STKaiti', size=10)) ## Error in `data1 %>% ggplot(aes(x = start, y = fct_reorder(item, start, .desc = TRUE)))`:
## ! could not find function "%>%"
发现图中有图重合现象,这是因为独立登录发生时间太过于相近。
处理的方法是将比较相近的相同登录,转为同一时间,函数为
mutate(start = as.Date(start))
然后加入jitter函数,
position=position_jitter(width=0.5,height=0)。
其中,height=0表示控制点不纵向扰动。
data1 %>%
mutate(start = as.Date(start)) %>%
ggplot(aes(x = start, y = fct_reorder(item, start, .desc = TRUE))
) +
geom_point(
position=position_jitter(width=0.5,height=0),
size = 6,
alpha = 0.5,
col = 'gold'
) +
xlab(NULL) +
ylab(NULL) +
theme_light() +
theme(
legend.position = "none",
panel.border = element_blank(),
) +
theme(text = element_text(family = 'STKaiti', size=10)) ## Error in `data1 %>% mutate(start = as.Date(start)) %>% ggplot(aes(x = start, y = fct_reorder(
## item, start, .desc = TRUE)))`:
## ! could not find function "%>%"