1

Based on this code:

ggplot(inputR_performances, aes(x=reorder(X,MCCHVAR))) + 
  geom_point(aes(y=MCCGS, col="chartreuse4")) +
  geom_point(aes(y=MCCHVAR, col="cyan2")) +
  geom_point(aes(y=MCCHDIV, col="cornflowerblue")) +
  geom_smooth(aes(y=MCCHVAR, group=1), method="loess", se=FALSE, col="cyan2") + 
  geom_smooth(aes(y=MCCHDIV, group=1), method="loess", se=FALSE, col="cornflowerblue") + 
  geom_smooth(aes(y=MCCGS, group=1), method="loess", se=FALSE, col="chartreuse4") +
  panel.configuration

That gives: enter image description here What should I change so that:

  1. the dots appear in the expected (custom) colour and not the pink-green-blue default?
  2. the legend labels are "MCC.." and not "chartreuse4..."?

First I tried placing col=... outside aes in geom_point, like it appears in geom_smooth. That gave the right colour but no legend was shown. Thanks.

UPDATE: After @beetroot comment:

Starting data: Dataframe with 4 columns (one for X and 3 for Y: X [factors], MCCGS [numeric], MCCHVAR [numeric], MCCHDIV [numeric]):

colnames(df1input) <- c("X","MCCGS","MCCHVAR","MCCHDIV")    

I changed it to a 3 column dataframe using melt function so that all numeric values are in one column (column called "value") and a column ("variable") indicates whether it comes from MCCHVAR, MCCHDIV, etc.

df1input_m <- melt(df1input, id="X") 

str(df1input_m)
'data.frame':   204 obs. of  3 variables:
 $ X       : Factor w/ 68 levels "O00255","O15118",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ variable: Factor w/ 3 levels "MCCGS","MCCHVAR",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value   : num  0.78 0.49 0.83 0.69 0.74 0.54 0.48 0.57 0.69 0.84 ...
PGreen
  • 3,239
  • 3
  • 24
  • 29
  • 2
    you should reshape your data to long format, such that you only have one `geom_point` and one `geom_smooth`, each grouped by the y-variable. – erc Aug 20 '14 at 09:24
  • Thanks @beetroot. How can `reorder(X,MCCHVAR)` now? I've updated the new df structure – PGreen Aug 20 '14 at 10:02
  • I'm sorry but I don't understand your data structure from what you have provided. – erc Aug 20 '14 at 12:25
  • @beetroot I've edited the update. I hope it's more clear now. – PGreen Aug 20 '14 at 14:01

2 Answers2

1

According to the color, I guess that you just add the color-aes to the ggplot-command, so something like ggplot(data, aes(x=X, y=value, color=variable))+geom_point()+geom_smooth() should do what you want.

The legend title, for instance, can be changed with a scale-parameter: scale_colour_discrete(name="legend title")

I'm not sure, but perhaps the sjp.scatter function of the sjPlot-package also does what you like to do (see an example here).

Daniel
  • 7,252
  • 6
  • 26
  • 38
  • Thanks, Daniel. The plot that I showed has the expected format (x-axis labels are sorted according to MCCHVAR) and all y-values (MCCHVAR, MCCHDIV and MCCGS) are in the same plot. with this new melted df I don't know how to sort x-axis according to MCCHVAR values and how to plot three different geom_smooth, one for each MCC, since your suggestion prints points in three different colors but a single smooth curve. Any clue? Thanks again – PGreen Aug 21 '14 at 11:56
  • I'm not sure I can build a reproducable example and thus I don't excactly know how you like to order your x axis? `x` is currently a factor with 68 levels, but what is the sorting criteria? – Daniel Aug 26 '14 at 09:56
  • According to the smooth-geom: I have used following in my function: `stat_smooth(data=df, aes(colour=grp))`, perhaps you should simply replace your `geom_smooth` with `stat_smooth(data=performances, aes(colour=variable))`? – Daniel Aug 26 '14 at 09:57
0

Adding "col" in the aesthetics only works when you want to colour it by a variable. Removing the colour command from your current code will also make the legend names revert to their default.

As for colouring the points, simply move the col out of the aes part. This code should do it for you:

ggplot(inputR_performances, aes(x=reorder(X,MCCHVAR))) + 
  geom_point(col="chartreuse4", aes(y=MCCGS)) +
  geom_point(col="cyan2", aes(y=MCCHVAR)) +
  geom_point(col="cornflowerblue", aes(y=MCCHDIV)) +
  geom_smooth(col="cyan2", aes(y=MCCHVAR, group=1), method="loess", se=FALSE) + 
  geom_smooth(col="cornflowerblue", aes(y=MCCHDIV, group=1), method="loess", se=FALSE) + 
  geom_smooth(col="chartreuse4", aes(y=MCCGS, group=1), method="loess", se=FALSE) +
  panel.configuration

So essentially, your problem is actually that the legend does not appear. Try finding other SO topics that had this issue (e.g. ggplot2: how to show the legend, Missing legend with ggplot2 and geom_line, Add legend to ggplot2 line plot), or make a new thread

Community
  • 1
  • 1
thestral
  • 491
  • 1
  • 4
  • 16