In my example I created a second y-axis in ggplot.
Instead of changing the colour for the data points of the second y-axis I want to change the shape.
But, of course, I get the error Scale for 'shape' is already present. Adding another scale for 'shape', which will replace the existing scale.
In the end I want to have a circle for "Large" and a filled circle for "Small" for "Visits" of the second y-axis.
This is my code:
Treatment <- c(rep(c("T/T"), times = 6),rep(c("R/R"), times = 6))
Size <- c(rep(c("Large", "Large", "Large", "Small", "Small", "Small"), times = 2))
Areatime <- c(240, 220, 120, 60, 55, 75, 90, 45, 70, 115, 40, 30)
Visits <- c(30, 45, 45, 65, 55, 55, 25, 35, 45, 75, 65, 55)
Counts <- data.frame(Treatment, Size, Areatime, Visits)
Counts$Treatment <- factor(Counts$Treatment, levels=c("T/T","R/R"))
Counts$inter <- interaction(Counts$Treatment, Counts$Size)
str(Counts)
ggplot(Count, aes(x = Treatment, y = Areatime)) +
geom_jitter(aes(y = Areatime, shape = Size, width = 0.3)) +
scale_shape_manual(values = c(0, 15), name = "Areatime", breaks = c("Large", "Small"),
labels = c("Large", "Small")) +
geom_jitter(aes(y = Counts*3, colour = Size, width = 0.3)) +
scale_y_continuous(sec.axis = sec_axis(~./3, name = "Relative Visits [%]"))+
scale_colour_manual(values =c(1, 2), name="Visits", breaks=c("Large", "Small"),
labels=c("Large", "Small")) +
theme(axis.text.x=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks.x = element_blank(),
axis.line.x = element_blank(),
axis.line.y = element_line("black")) +
facet_grid(.~Treatment, scales="free", switch = "x") +
labs(y = "Average Areatime [sec]")