Colouring a particular class - r

library(ggplot2)
ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point()
If I want to colour a particular class, let us say 2 seater, what is the modification in the code

Try this. You can wrap the logical condition directly inside colour element:
library(ggplot2)
ggplot(mpg, aes(displ, hwy, colour = (class=='2seater'))) +
geom_point()+
labs(color='class')+
scale_color_discrete(labels=c('TRUE'='2seater','FALSE'='other'))
Output:
As legend will not have a fashion title, you can use labs() to change that or follow the pretty smart advice from #r2evans creating a new column to store the results of the logical condition using dplyr:
library(dplyr)
#Code
mpg %>%
mutate(Color=ifelse(class=='2seater','2seater','Other')) %>%
ggplot(aes(displ, hwy, colour = Color)) +
geom_point()
Output:
Update:
#Code2
mpg %>% mutate(Color=ifelse(class=='2seater','2seater','Other')) %>%
ggplot(aes(displ, hwy, colour = Color)) +
geom_point()+
scale_color_manual(values = c("2seater" = "#992399", "Other" = "#000000"))+
geom_smooth(method = 'lm',aes(group=1),show.legend = F)
Output:

You can try adding specific colors this way too:
ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point() +
scale_color_manual(values = c("2seater" = "#992399", "Other" = "#000000"))

Related

ggplot, geom_point color based on a variable

I'm looking for a way to color points of a plot based on a variable 'var_color'.
This is an example code:
library(tidyverse)
library(datasets)
var_color <- manufacturer
mpg %>%
ggplot(mapping = aes(x = displ, y = hwy, color = var_color)
) +
geom_point()
Does anyone know how can I do it?
Greetings
Here is a way. Use get to get the value of the variable var_color. Then change the legend title.
library(ggplot2)
var_color <- "manufacturer"
ggplot(mpg, mapping = aes(x = displ, y = hwy, color = get(var_color))) +
geom_point() +
guides(color = guide_legend(title = var_color))

trendline in R in ggplot

mpg %>%
mutate(Color=ifelse(class=='2seater','2seater','Other')) %>%
ggplot(aes(displ, hwy, colour = Color)) +
geom_point() +
scale_color_manual(values = c("2seater" = "#992399", "Other" = "#000000"))
To this I am trying to add a trend line which is for all categories because , if I add a trend line geom_smooth(method="lm"), it draws for 2 seater separately which I don't want
Override the colour aesthetic, which is the grouping aesthetic, with group = 1 in the call to geom_smooth.
library(tidyverse)
mpg %>%
mutate(Color=ifelse(class=='2seater','2seater','Other')) %>%
ggplot(aes(displ, hwy, colour = Color)) +
geom_point() +
scale_color_manual(values = c("2seater" = "#992399", "Other" = "#000000")) +
geom_smooth(aes(group = 1),
method = "lm", formula = y ~ x)
The geom_smooth inherits the aes arguments from ggplot. You can move 'colour' to geom_point, or pass inherit.aes = F to geom_smooth.
mpg %>%
mutate(Color=ifelse(class=='2seater','2seater','Other')) %>%
ggplot(aes(displ, hwy)) +
geom_point(aes(, colour = Color)) +
scale_color_manual(values = c("2seater" = "#992399", "Other" = "#000000")) + geom_smooth(method = 'lm')
#or:
mpg %>%
mutate(Color=ifelse(class=='2seater','2seater','Other')) %>%
ggplot(aes(displ, hwy, colour = Color)) +
geom_point() +
scale_color_manual(values = c("2seater" = "#992399", "Other" = "#000000")) + geom_smooth(method = 'lm', inherit.aes = F, aes(displ, hwy))

Colour scheme in a separate dataframe

I can specify the colors in a plot by using scale_color_manual as below:
library(tidyverse)
mpg %>%
filter(class=="2seater"|class=="minivan")%>%
ggplot(aes(displ, hwy,colour=class)) +
geom_point()+
scale_color_manual(values=c(
"2seater"="green",
"minivan"="red"))
But if I had a separate dataframe as below:
class<-c("2seater","minivan")
color<-c("green","red")
colorscheme<-data.frame(class,color,stringsAsFactors = FALSE)
How can I use this to specify the colors within the ggplot?
mpg %>%
filter(class %in% c("2seater", "minivan")) %>%
ggplot(aes(displ, hwy, color = class)) +
geom_point() +
scale_color_manual(values = colorscheme$color,
labels = colorscheme$class)
Another option can be scale_color_identity() after joining:
library(tidyverse)
#Code
mpg%>%filter(class=="2seater"|class=="minivan")%>%
left_join(colorscheme) %>%
ggplot(aes(displ, hwy,colour=color)) +
geom_point()+
scale_color_identity(guide = "legend",
labels=c("2seater","minivan"),name='class')
Output:

ggplot labels with "K" for thousands or "M" for millions (maintaining 'comma' y-axis labeling)

library(tidyverse)
df <- mpg %>% head() %>% mutate(hwy = hwy * 10000)
ggplot(df, aes(cty, hwy)) +
geom_point() +
scale_y_continuous(label = scales::comma) +
geom_text(aes(label = hwy), hjust = -0.25)
I want the labels on this plot to use "K" for thousands (eg 260K instead of 260000). BUT - I want to maintain the y-axis as is, with commas (eg 260,000). How can I achieve this?
You can use scales::label_number_si():
library(scales)
library(ggplot2)
ggplot(df, aes(cty, hwy)) +
geom_point() +
scale_y_continuous(label = comma) +
geom_text(aes(label = label_number_si()(hwy)), hjust = -0.25)
You can just add a custom column called myLabel to your dataframe which holds your desired labels. In the package scales you can find a function that does the converting part for you:
df <- mpg %>% head() %>% mutate(hwy = hwy * 10000)
df$myLabels <- scales::label_number_si()(df$hwy)
Now, use the new column myLabels as the aesthetic for creating the labels
ggplot(df, aes(cty, hwy)) +
geom_point() +
scale_y_continuous(label = scales::comma) +
geom_text(aes(label = myLabels), hjust = -0.25)

Matching legends for ggplot2

I refer to section 12.2.4, exercise 1 in Hadley Wickham's ggplot2 book.
In the exercise, the following code is provided:
fwd <- subset(mpg, drv == "f")
rwd <- subset(mpg, drv == "r")
ggplot(fwd, aes(displ, hwy, colour = class)) + geom_point()
ggplot(rwd, aes(displ, hwy, colour = class)) + geom_point()
Which produces the following output:
The following task is given: Modify the code so that the legend and axes match, without using facetting!
I am able to modify so that the axes match:
ggplot(fwd, aes(displ, hwy, colour = class)) + geom_point() + xlim(c(0, 7)) + ylim(c(0, 45))
ggplot(rwd, aes(displ, hwy, colour = class)) + geom_point() + xlim(c(0, 7)) + ylim(c(0, 45))
yielding:
But, I'm unable to get the legends to match. This question comes before scale_colour_manual() is explained, so I don't think we're supposed to use that.
Any ideas about how to get the legends to match?
Thanks!

Resources