0

I am trying to change have separate colors for my lines and points. My data is split by Arm so at each time-point there should be two dots and two lines connecting them to the previous and future time-point.

I can get both the line and dot colors to change together, but I would like the line to be a different colour, still based on Arm though. As in, I want the lines to be light blue for Arm=1 and yellow for Arm=2, but the dots to stay they color shown below. Is this possible with ggplot?

Any help would be much appreciated.

What I have: enter image description here

Code:

ggplot(head(TOT, 12), aes(x=VisitNo, y=Mean)) +
  geom_line(size=1.5, aes(color=as.factor(Arm))) +
  geom_point(size=3, aes(color=as.factor(Arm))) +
  scale_colour_manual(values = c("blue", "orange")) +
  theme_bw()

Data:

TOT <- structure(list(Arm = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), 
        VisitNo = structure(c(0L, 6L, 12L, 16L, 24L, 36L, 0L, 6L, 12L, 16L, 24L, 36L), 
        label = "VisitNo", class = c("labelled", "integer")), 
        variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
        .Label = c("PWB", "SWB", "EWB", "FWB", "AC"), class = "factor"), 
        Mean = c(25.3025326086957, 25.4365119047619, 25.8333333333333, 21.3452380952381, 
                 26, 26.8235294117647, 25.2272727272727, 25.6172839506173, 
                 25.6805555555556, 21.625976744186, 26.24, 26)), 
        row.names = c(NA, 12L), class = "data.frame")
morgan121
  • 2,213
  • 1
  • 15
  • 33

1 Answers1

1

If you just want the lines to be a bit lighter than the points, you can use alpha to make the lines a bit transparent:

ggplot(head(TOT, 12), aes(x=VisitNo, y=Mean)) +
    geom_line(size=1.5, aes(color=as.factor(Arm)), alpha = 0.4) +
    geom_point(size=3, aes(color=as.factor(Arm))) +
    scale_colour_manual(values = c("blue", "orange")) +
    theme_bw()
Marius
  • 58,213
  • 16
  • 107
  • 105
  • For the future though, if I wanted the lines to be completely separate colours is there a way to do that? – morgan121 Feb 13 '19 at 04:40
  • Yes, some points have both a colour (border) and fill (center), so you can use something like `shape = 21` in `geom_point()` and then use a fill scale for the points and a colour scale for the lines. – Marius Feb 13 '19 at 04:43
  • 1
    That approach is shown here: https://stackoverflow.com/a/29531292/1222578 – Marius Feb 13 '19 at 04:44