0

I have a dataframe:

df <- data.frame(Name=c('abc', 'bcd', 'cde', 'bcd', 'abc', 'def'), Pos=c(1, 2, 3, 2, 4, 5))

Name  Pos
abc   1
bcd   2
cde   3
bcd   2
abc   4
def   5

I want to plot the names in order of their position, but I want each row to appear, even if it's a duplicated value:

ggplot(df, aes(Pos, reorder(Name, -Pos))) +
    geom_jitter(width=0, height=0.05)

enter image description here

I can see that 'bcd' has two points at Pos 2, and 'abc' has a point at Pos 1 and 4. But I would like these to be on separate ticks on the y axis. If I change df$Name to a character instead of a factor (df$Name <- as.character(df$Name)), this doesn't help.

Is there a way to do this?

1
  • Maybe create a new y axis variable with actual unique values, and then just modify the labels so some are repeated? Commented Apr 4, 2018 at 19:16

2 Answers 2

1

This adds the row numbers to the data.frame, and plots with these, but assigns the Names as labels. Is this what you had in mind?

library(tidyverse)
df <- data.frame(Name=c('abc', 'bcd', 'cde', 'bcd', 'abc', 'def'), Pos=c(1, 2, 3, 2, 4, 5)) %>% 
  mutate(n = 1:n())

ggplot(df, aes(Pos, n)) +
  geom_point() +
  scale_y_continuous(breaks = df$n, labels = df$Name)
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe like this...?

library(dplyr)

df <- df %>% 
  group_by(Name,Pos) %>% 
  mutate(Name1 = paste0(Name,row_number()))
labs <- with(df,setNames(as.character(Name),Name1))

ggplot(df, aes(Pos, reorder(Name1, -Pos))) +
  geom_jitter(width=0, height=0.05) + 
  scale_y_discrete(labels = labs)

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.