Reputation: 3307
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: What should I change so that:
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 ...
Upvotes: 1
Views: 1412
Reputation: 551
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
Upvotes: 0
Reputation: 7822
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).
Upvotes: 1