How to generate the same story with “jitter” and how to jitter selected points (not all points)?

What I would like to do:

a) does the graph created by the code ggplothave every time it runs the [set.seed concept type?] and

b) have text labels that tremble only for labels that have the same y-axis value - leave other text labels separately. It would seem that this is some kind of conditional jitter based on the value of the coefficient for points.

Here are some details:

dput(df)
structure(list(Firm = c("a verylongname", "b verylongname", "c verylongname", 
"d verylongname", "e verylongname", "f verylongname", "g verylongname", 
"h verylongname", "i verylongname", "j verylongname"), Sum = c(74, 
77, 79, 82, 85, 85, 88, 90, 90, 92)), .Names = c("Firm", "Sum"
), row.names = c(NA, 10L), class = "data.frame")

Here is the code ggplotusing df:

ggplot(df, aes(x = reorder(Firm, Sum, mean), y = Sum)) +
  geom_text(aes(label = Firm), size = 3, show.guides = FALSE, position = position_jitter(height = .9)) +
  theme(axis.text.x = element_blank()) +
  scale_x_discrete(expand = c(-1.1, 0)) +   # to show the lower left name fully
  labs(x = "", y = "", title = "")

Please note that one version of the graph still overlaps h and I - every time I run the above code, the locations of the text labels change.

enter image description here

, x, () y .

+4
1

- , . y , . .

1 ():. . , , , y . , , x.

df$overlap = lapply(1:nrow(df), function(i) {
  if(min(abs(df[i, "Sum"] - df$Sum[-i])) <= 1) "Overlap" else "Ignore"
})

, , .

# Add set.seed() here to make jitter reproducible
ggplot(df, aes(x = reorder(Firm, Sum, mean))) +
  geom_text(data=df[df$overlap=="Overlap",], 
            aes(label = Firm, y = Sum), size = 3,  
            position = position_jitter(width=0, height = 1), colour="red") +
  geom_text(data=df[df$overlap=="Ignore",], 
            aes(label = Firm, y = Sum), size = 3) +
  theme(axis.text.x = element_blank()) +
  scale_x_discrete(expand = c(-1.1, 0)) +   # to show the lower left name fully
  labs(x = "", y = "", title = "")

enter image description here

2 ( ): - , , , -, jitter . , y. , x y, / , .

library(dplyr)

# Create a new column that shifts pairs of points with the same y-value by +/- 0.25
df = df %>% group_by(Sum) %>%
  mutate(SumNoOverlap = if(n()>1) Sum + c(-0.25,0.25) else Sum)

ggplot(df, aes(x = reorder(Firm, Sum, mean), y = SumNoOverlap)) +
  geom_text(aes(label = Firm), size = 3) +
  theme(axis.text.x = element_blank()) +
  scale_x_discrete(expand = c(-1.1, 0)) +   # to show the lower left name fully
  labs(x = "", y = "", title = "")

enter image description here

.. , set.seed(153) ( ) .

+4

Source: https://habr.com/ru/post/1606990/


All Articles