0

I have a data frame of products and time to produce them (days):

test <- structure(list(product = c("prod.1", "prod.2", "prod.3", "prod.4", "prod.5",
"prod.6", "prod.7", "prod.8", "prod.9", "prod.10", "prod.11", "prod.12", "prod.13", 
"prod.14", "prod.15", "prod.16", "prod.17", "prod.18", "prod.19", "prod.20", "prod.21", 
"prod.22", "prod.23", "prod.24", "prod.25", "prod.26", "prod.27", "prod.28", "prod.29", 
"prod.30", "prod.31", "prod.32", "prod.33", "prod.34", "prod.35", "prod.36", "prod.37", 
"prod.38", "prod.39", "prod.40", "prod.41", "prod.42", "prod.43", "prod.44", "prod.45", 
"prod.46", "prod.47", "prod.48", "prod.49", "prod.50"), prodTime = c(4.03, 3.8, 3.75, 
3.74, 3.72, 3.7, 3.66, 3.66, 3.66, 3.64, 3.63, 3.63, 3.63, 3.6, 3.6, 3.58, 3.58, 3.58, 
3.57, 3.57, 3.57, 3.56, 3.56, 3.56, 3.56, 3.55, 3.53, 3.53, 3.53, 3.53, 3.53, 3.52, 3.51, 
3.51, 3.51, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.49, 3.49, 3.48, 3.48, 3.48, 3.48, 
3.47)), .Names = c("product", "prodTime"), row.names = c(NA, -50L), class = 
c("data.frame"))

and have no problem to output a plot which is almost as I would like it:

ggplot(data=test, aes(x=prodTime, y=reorder(factor(product), prodTime))) +
  geom_segment(aes(yend=product, xend=0)) + 
  geom_point() + 
  coord_cartesian(xlim=c(2.5, 4.25)) + 
  theme_minimal() + 
  theme(panel.grid.major.y=element_blank(), 
        axis.ticks=element_blank()) + 
  labs(x='', y='') + 
  geom_vline(aes(xintercept=3.7), color = 'red', linetype = 'dashed') +
  geom_vline(aes(xintercept=3.3), color = 'blue', linetype = 'dashed')

However, what I am trying to get is a similar plot where all product labels (prod.01 to prod.50), segments and points would be very light grey but a couple of them would be highlighted (for instance, in a different color). Say prod.03, prod.12, prod.29, prod.41 to be the only ones (labels, segments and points) to be highlighted.

Any suggestion is most welcomed.

1 Answer 1

5

Add new column to your data frame where products are divided in two groups (should be highlighted or not).

test$high<-ifelse(test$product %in% c("prod.3", "prod.12", "prod.29", "prod.41"),"Yes","No")

Then use this new column for color= in aes() of ggplot(). Colors you can change with scale_color_manual().

ggplot(data=test,aes(x=prodTime,y=reorder(factor(product),prodTime),color=high)) + 
      geom_segment(aes(yend=product, xend=0)) + geom_point() + 
      coord_cartesian(xlim=c(2.5, 4.25)) + theme_minimal() + 
      theme(panel.grid.major.y=element_blank(), axis.ticks=element_blank()) + 
      labs(x='', y='') + 
      geom_vline(aes(xintercept=3.7), color = 'red', linetype = 'dashed') +
      geom_vline(aes(xintercept=3.3), color = 'blue', linetype = 'dashed')+
      scale_color_manual(values=c("grey50","red"),guide=FALSE)

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

easy, actually ! Thank you very much

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.