ggplot2 colour function isn't working

This is my code:

ggplot(data = covid, aes(x=`medinc1317`+1,y=`population`+1))+
  geom_point()+
  scale_x_log10("Median Income", breaks=c(1,30001,60001,90001,120001), labels=c("0","30000","60000","90000","120000"))+ 
  scale_y_log10("County Population",breaks=c(1,1001,10001,100001,1000001), labels=c("0","1000","10000","100000","1000000"))+
  ggtitle("Covid-19 Cases as a Proportion of County Population vs Median Income")

I'm trying to colour the data for the x axis and y axis different colours, but the colour function isn't doing anything (doesn't even pop up!). Any help would be appreciated.

Are you trying to color the axis lines and labels? I would do that like this:

DF <- data.frame(Xval = 1:5, Yval = 1:5)
library(ggplot2)
ggplot(DF, aes(Xval, Yval)) + geom_point() + 
  theme(axis.line.x = element_line(color = "green"),
        axis.text.x = element_text(color = "green"),
        axis.line.y = element_line(color = "red"),
        axis.text.y = element_text(color = "red"))

Not sure what you mean by:

I'm trying to colour the data for the x axis and y axis different colours

Your data points can't be colored differently for the x-axis and the y-axis because each point lies somewhere along both axis.

You would usually pick a categorical variable in your data to which to color the different points by. You can add the column as an argument in aes(colour=variable) or you can add it in geom_point(aes(colour=variable)).

Happy to help more if you could clarify on what you meant there!

Thanks! I’m trying to use the “colour” function, but when I type it in only the “colours” function pops up. Not sure what to do.

Hmm that's weird, I'm actually seeing the same on my end, not sure why the option isn't showing up and I agree it should. You can still add it into aes() regardless, here is an example:

# Making a 2 row example dataframe to use for a simple ggplot
example_data <- data.frame(x=c(1,2), y=c(2,3), color_column=c('text1', 'text2'))
# See below on where to place `colour=`
ggplot(example_data, aes(x = x, y = y, colour = color_column)) + geom_point(aes())

And you should see a chart that looks like this:

Try to see where I placed colour=c and try to replicate that for your data.

You can also wrap it in geom_point(aes()) like this:

ggplot(example_data, aes(x = x, y = y)) + geom_point(aes(colour = color_column))

Which gives the same result.

Let me know if this doesn't get you all the way there and I'd be happy to help! If you need more help I would also recommend looking at "Scenario 2" from this link on how you can give us an example from your data to work with: https://reprex.tidyverse.org/articles/articles/datapasta-reprex.html

colour is actually an argument to the aes function rather than a function itself. However, because it's an optional argument, when you type aes( and then type tab, this is what appears:

Screen Shot 2020-10-04 at 1.41.24 PM

The ... argument is the stand-in for the optional arguments that can be passed to aes.

Normally, with ggplot you would map a data column to the colour argument, as @ries9112 showed in his answer. Occasionally, you might want to create a colour aesthetic without using a data column, as shown, for example, in this StackOverflow answer.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.