0

I created a ggplot2 plot where I struggle with 2 things.

1) How to change the colors?

2) Why is the line missing between items 9 and 10?

cent <- rnorm(n=20, mean=5, sd=1)
num <- c(1:20)
groups2 <- c(rep("DSM Symptoms",9),rep("Non-DSM Symptoms",11))
data2 <- data.frame(num, cent, groups2)

ggplot(data2, aes(x=num, y=cent, fill=groups2, colour=groups2)) + 
  geom_line(color='#666666', size=0.7) +
  geom_point() +
  ylab('Strength Centrality') + xlab ('Symptoms') + 
  scale_x_reverse() +
  scale_x_continuous(breaks = c(1:20)) +
  coord_flip() +
  theme_bw() +
  theme(panel.grid.minor.y = element_blank())

screenshot

3
  • 1
    Remove the fill in the aes if you want a single line. scale_color_manual(values=c("green","yellow")) If you want to change the colour of the dots to green and yellow for example. Commented Nov 1, 2016 at 19:39
  • 1
    To connect the lines, use, group = 1 within geom_line -- geom_line(color='#666666', size=0.7, group = 1). You can use scale_color_manual to change the colors, scale_color_manual(values = c("green", "orange")). Commented Nov 1, 2016 at 19:42
  • 1
    Also, scale_x_reverse is getting overridden by scale_x_continuous. Use one or the other. Commented Nov 1, 2016 at 19:52

1 Answer 1

1

Working example:

ggplot(data2, aes(x=num, y=cent, colour=groups2)) + 
   geom_line(color='#666666', size=0.7) +
   geom_point() +
   ylab('Strength Centrality') + xlab ('Symptoms') + 
   scale_x_continuous(breaks = c(20:1)) +
   coord_flip() +
   theme_bw() + scale_color_manual(values=c("cyan","green"))+
   theme(panel.grid.minor.y = element_blank())

note the scale_color_manual(values=c(...)) and the absence of the fill parameter in the aesthetics section.

enter image description here

Note that if you want to reverse the ordering, you'd need to do something like scale_x_reverse(breaks=c(1:20)) since the scale_x_... options overwrite each other, with only the latest implemented.

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

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.