geom_point - change colour of variable - r

I coloured my data by "Full Bath", however it is a similar shade & unclear.
I am trying to change the colour by variable of "Full Bath" to be more clear.
Could anyone help :)
This is what I have:
data %>%
ggplot(aes(Sale.Price, Total.Bsmt.SF)) +
geom_point(aes(colour = Full.Bath)) + geom_smooth()

To manually change the colors, you can use scale_color_manual, as follows:
### Simulation of data
set.seed(1)
data <- data.frame(Sale.Price=rnorm(100, 100, 10),
Total.Bsmt.SF=rnorm(100, 10, 1),
Full.Bath=rep(c("a", "b", "c", "d", "e"), 20))
### Plot data
data %>%
ggplot(aes(x = Sale.Price, y = Total.Bsmt.SF)) +
geom_point(aes(colour = Full.Bath)) +
geom_smooth() +
scale_color_manual(values=c("#84d58d", "#84c1d5", "#9e84d5", "#d584b0", "#d5be84"))
Link for colors selection
https://htmlcolorcodes.com/fr/

Related

Legend for scatter-line graph in ggplot2 (without colour)

very new to this so apologies if there's an obvious answer. I'm trying to add a legend to a scatter-line graph with 2 y variables; i'm aware this can be done using colour, however I ideally want to keep this black and white, and define the variables in the legend by linetype/point instead. Is there any way to do this?
ggplot(birds, aes(distance)) +geom_point(aes(y=individuals_AC)) +geom_point(aes(y=species_AC, shape=17)) +geom_line(aes(y=individuals_AC)) +geom_line(aes(y=species_AC, linetype="dashed")) + scale_shape_identity() + scale_linetype_identity() + theme_classic()
library(tidyverse)
#create some dummy data
df <- tibble(
x = runif(10),
y = runif(10),
type = rep(c("a", "b"), 5)
)
#plot it with a different shape for each type
df %>%
ggplot(aes(x, y, shape = type)) +
geom_point()

Assign custom colors to each plot of facet_wrap histograms in R - ggplot

I want to use facet_wrap in R to split my plots based on a certain column. Here is a working example I reproduced from here:
set.seed(1)
df <- data.frame(age = runif(500, min = 10, max = 100),
group = rep(c("a", "b", "c", "d", "e"), 100))
#Plotting
ggplot(df, aes(age)) +
geom_histogram(aes(y = (..count..)), binwidth = 5) +
facet_wrap(~group, ncol = 3)
This produces plots, all in grey color (shown below). However, I want each plot be in a specific color. That is, they have the following color in order c("green","orange","blue","black", "red"). All bars in plot (a) be green, all in (b) be orange, and so on. These colors match my other plots and preserve consistency.
How can I achieve this task?
Thanks.
ggplot(df, aes(age)) +
geom_histogram(aes(y = (..count..), fill=group), binwidth = 5) +
facet_wrap(~group, ncol = 3) +
scale_fill_manual(values=c("green","orange","blue","black", "red"))

Individual legends for separate geom_line aesthetics in the same ggplot

I'm new to R and I'm trying to create a single plot with data from 2 melted dataframes.
Ideally I would have a legend for each of the dataframes with their respective titles; however, I get a only a single legend with the title of the first aesthetic.
My starting point is:
aerobic_melt <- melt(aerobic, id.vars = 'Distance', variable.name = 'Aerobic')
anaerobic_melt <- melt(anaerobic, id.vars = 'Distance', variable.name = 'Anaerobic')
plot <- ggplot() +
geom_line(data = aerobic_melt, aes(Distance, value, col=Aerobic)) +
geom_line(data = anaerobic_melt, aes(Distance, value, col= Anaerobic)) +
xlim(0, 125) +
ylab('Energy (J/kg )') +
xlab('Distance (m)')
Which results in
I've searched, but with my limited ability I haven't been able to find a way to do it.
My question is:
How do I create separate legends with titles 'Aerobic' and 'Anaerobic' which should respectively refer to A,B,C,F,G,L and E,H,I,J,K?
Any help is appreciated.
Obviously we don't have your data, but I have created some sample data that should have the same names and structure as your own data frames, since it works with your own plot code. See the end of the answer for the data used here.
You can use the package ggnewscale if you want two color scales on the same plot. Just add in a new_scale_color() call between your geom_line calls. I have left the rest of your code as-is.
library(ggplot2)
library(ggnewscale)
plot <- ggplot() +
geom_line(data = aerobic_melt, aes(Distance, value, col=Aerobic)) +
new_scale_color() +
geom_line(data = anaerobic_melt, aes(Distance, value, col= Anaerobic)) +
xlim(0, 125) +
ylab('Energy (J/kg )') +
xlab('Distance (m)')
plot
Data
set.seed(1)
aerobic_melt <- data.frame(
Aerobic = rep(c("A", "B", "C", "F", "G", "L"), each = 120),
value = as.numeric(replicate(6, cumsum(rnorm(120)))),
Distance = rep(1:120, 6))
anaerobic_melt <- data.frame(
Anaerobic = rep(c("E", "H", "I", "J", "K"), each = 120),
value = as.numeric(replicate(5, cumsum(rnorm(120)))),
Distance = rep(1:120, 5))

Highlight / Draw a box around some of the plots when using `facet_grid` in ggplot2

I am creating a matrix of plots similar to
ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(rows = vars(cyl), cols = vars(drv))
Now, I would like to have some way to highlight some of the individual plots, say the ones where cyl is 5 or 6, and drv is f. So, ideally, this might look like this:
But I would also be happy with those panels having a different look by setting ggtheme to classic or similar.
However, it is very unclear to me how I can modify individually selected plots within a matrix of plots generated via facet_grid
From #joran answer found here, this is what I get :
[EDIT] code edited to select multiple facets
if(!require(tidyverse)){install.packages("tidyverse")}
library(tidyverse)
#dummy dataset
df = data.frame(type = as.character(c("a", "b", "c", "d")),
id = as.character(c("M5", "G5", "A7", "S3")),
val = runif(4, min = 1, max = 10),
temp = runif(4))
# use a rectangle to individually select plots
ggplot(data = df, aes(x = val, y = temp)) +
geom_point() +
geom_rect(data = subset(df, type %in% c("b", "c") & id %in% c("A7","G5")),
fill = NA, colour = "red", xmin = -Inf,xmax = Inf,
ymin = -Inf,ymax = Inf) +
facet_grid(type~id)
It does not use theme() but it seems simple enough to highlight some facets.

Add multi-stack axes label to plot

I have a dataset, named “data”:
df=ddply(data,c("Treatment","Concentration"),summarise,mean=mean(Inhibition),sd=sd(Inhibition),n=length(Inhibition),se=sd/sqrt(n))
p <- ggplot(df, aes(x=Treatment, y=Inhibition))
p1 <- p + geom_bar(stat="identity", position="dodge") +
geom_errorbar(aes(ymin=Inhibition-se,ymax=Inhibition+se), position="dodge",width=0.2)
and I got the following graph:
I want x-axis to be like the picture below:
How woud I do this??
This is best achieved using a facet within ggplot. As you haven’t included a reusable dataset, I have made one here:
df <- data.frame(Group = c("A", "A", "A", "A", "B"),
SubGroup = c(letters[1:5]),
value = 1:5
)
See below the facet_grid line which has a few additional options specified. You can read more about the added arguments here
library(ggplot2)
ggplot(df, aes(x = SubGroup, value)) +
geom_bar(stat="identity", position="dodge") +
facet_grid(.~Group, scales = "free_x", space = "free", switch = "x") +
theme(strip.placement = "outside")
For your data, you will need to split the drug and dose into two separate columns first, like my example.

Resources