Same code different plot in qplot vs ggplot - r

I get very different results, the code should be equivalent?
qplot(x = price, data = diamonds) + facet_wrap(~cut)
vs
ggplot(aes(x = diamonds$price), data = diamonds) + geom_histogram() + facet_wrap(~cut)

try this instead
ggplot(aes(x = price), data = diamonds) + geom_histogram() + facet_wrap(~cut)

Related

Customize ggplot2 legend with different variables

I have the following data about American and German teenagers' coding skills. I can easily display their bar plots, but I need to present the total number of teenagers from each country as well.
DF <- data.frame(code = rep(c("A","B","C"), each = 2),
Freq = c(441,121,700,866,45,95),
Country = rep(c("USA","Germany"),3),
Total = rep(c(1186,1082),3))
ggplot(DF, aes(code, Freq, fill = code)) + geom_bar(stat = "identity", alpha = 0.7) +
facet_wrap(~Country, scales = "free") +
theme_bw() +
theme(legend.position="none")
For example, instead of presenting the default legend for the code, I could replace it with the Country and the Total. Your help is appreciated
Here's what I would suggest:
library(dplyr); library(ggplot2)
DF %>%
add_count(Country, wt = Total) %>%
mutate(Country_total = paste0(Country, ": Total=", n)) %>%
ggplot(aes(code, Freq, fill = code)) + geom_bar(stat = "identity", alpha = 0.7) +
facet_wrap(~Country_total, scales = "free") +
theme_bw() +
theme(legend.position="none")
To do what you're requesting would take a different approach, since the data you're describing would not strictly be a ggplot2 legend (which explains how one of the variables is mapped to one of the graph aesthetics), rather it would be a table or annotation that is displayed alongside the plot. This could be generated separately and added to the figure using patchwork or grid packages.
For instance:
library(patchwork); library(gridExtra)
ggplot(DF, aes(code, Freq, fill = code)) + geom_bar(stat = "identity", alpha = 0.7) +
facet_wrap(~Country, scales = "free") +
theme_bw() +
theme(legend.position="none") +
tableGrob(count(DF, Country, wt = Total)) +
plot_layout(widths = c(2,1))

Trying to only display two decades on axis of box-plot in R

Hi!
This is how my plot looks at the moment...
Want to try and only display decades 1980 and 2010 in my box plots.
This is how my code looks now:
gender_race_income <- ggplot(data = gender_pay_gap, aes(x = factor(decade), y = income, colour
= sex)) +
geom_boxplot() +
coord_flip() +
facet_grid(rows = vars(race)) +
scale_y_log10() +
theme_bw() +
scale_color_brewer(palette = "Set2")
Thank you :)
welcome to Stack Overflow. You can subset your data using filter() from dplyr and then pass this onto the ggplot using the %>% pipe.
gender_pay_gap %>%
dplyr::filter(decade == 1980 | decade == 2010) %>%
ggplot(aes(x = factor(decade), y = income, colour = sex)) +
geom_boxplot() +
coord_flip() +
facet_grid(rows = vars(race)) +
scale_y_log10() +
theme_bw() +
scale_color_brewer(palette = "Set2")

Plotting in layers in R

I'm trying to plot individual regression lines for all of my experimental subjects (n=40) on the same plot where I show the overall regression line.
I can do the plots separately with ggplot, but I haven't found a way to superpose them on the same graph.
I can illustrate what I did with the iris data frame:
#first plot
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
theme_classic()
# second plot, grouped by species
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, colour =Species)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
theme_classic()
# and I've been trying things like this:
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
theme_classic() +
geom_point(aes(x = Sepal.Width, y = Sepal.Length, colour =Species))) +
stat_smooth(method = lm, se = FALSE) +
theme_classic()
which returns the message "Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?", so I get that this is not the right way to combine them, but what is?
How can I combine both graphs in one?
Thanks in advance!
Repeat the whole data and set Species to be something else ("Together") in example below. Attach the repeated data to the original data and just call the second plot.
d1 = iris
d2 = rbind(d1, transform(d1, Species = "Together"))
ggplot(d2, aes(x = Sepal.Width, y = Sepal.Length, colour =Species)) +
stat_smooth(method = lm, se = FALSE) +
geom_point(data = d1) +
theme_classic()
Similar to #d.b's answer, consider expanding the data frame with rbind, assigning an "All" category for Species and adjust for factor levels (so All shows at top on legend):
new_species_level <- c("All", unique(as.character(iris$Species)))
iris_expanded <- rbind(transform(iris, Species=factor("All", levels=new_species_level)),
transform(iris, Species=factor(Species, levels=new_species_level)))
ggplot(iris_expanded, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
theme_classic()

Adding smoother to ggplot 2

I have used the following code to create a plot in r using ggplot2:
g <- ggplot(newdata, aes(MVPAper, FMI) +
geom_smooth(method = 'lm'))
I then added the following:
p <- g + geom_point(aes(color = Age)) +
facet_grid(Age ~ .) +
stat_smooth(method = 'lm') +
theme_bw(base_family = 'Times')`
I am wanting to have a smoother for each of the four graphs I have created, using the facet grid to split the graph into four ages 8,9,12,and 15) can anyone assist with my code?
You don't need both geom_smooth() and stat_smooth(). Try this:
library(tidyverse)
df <- diamonds %>% filter(price < 10000, carat < 2.5)
g <- ggplot(df, aes(carat, price, color = cut))
g +
geom_point() +
geom_smooth(method = 'lm') +
facet_grid(cut ~ .) +
theme_bw()

ggplot error: Found object is not a stat

ggplot() +
geom_point(aes(x = Africa_set$Africa_Predict, y = Africa_set$Africa_Real), color ="red") +
geom_line(aes(x = Africa_set$Africa_Predict, y = predict(simplelm, newdata = Africa_set)),color="blue") +
labs(title = "Africa Population",fill="") +
xlab("Africa_set$Africa_Predict") +
ylab("Africa_set$Africa_Real")
Then show the error message:
Error: Found object is not a stat
How can fix this error?
It looks like you are trying to plot points with a fitted regression line on top. You can do this using:
library(ggplot2)
ggplot(iris, aes(Petal.Length, Petal.Width)) +
geom_point() +
geom_smooth(method = "lm")
Or, if you really do want to use the model you've stored ahead of time in a simplelm object like you have in your example, you could use augment from the broom package:
library(ggplot2)
library(broom)
simplelm <- lm(Petal.Width ~ Petal.Length, data = iris)
ggplot(data = augment(simplelm),
aes(Petal.Length, Petal.Width)) +
geom_point() +
geom_line(aes(Petal.Length, .fitted), color = "blue")

Resources