Error: geom_point requires the following missing aesthetics: x and y - r

If I use the following commands:
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point()
all works fine.
If I run the following commands:
library(ggplot2)
library(ggtern)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point()
I receive this error message:
Error: geom_point requires the following missing aesthetics: x and y

ggtern has a ggplot() function as well, so the duplicate name is messing it up. Best option is to load the packages in the opposite order (the last package loaded always wins when there are duplicate names), or try the following:
library(ggplot2)
library(ggtern)
p <- ggplot2::ggplot(mtcars, aes(wt, mpg))
p + ggplot2::geom_point()

I have restarted R and reloaded the packages many time, but the error remain. I work with a MacOS Big Sur 10.16, with R version 4.0.5 (2021-03-31), ggtern_3.3.0 and ggplot2_3.3.5.9000 –

Related

Why does ggplotly break when ggtern is loaded?

I was hoping to use plotly::ggplotly() to plot a graph produced with ggtern() with a tooltip feature. But loading the package ggtern caused ggplotly to throw the following error:
Error in inherits(theme[[element]], ggplot_global$element_tree[[element]]$class) :
'what' must be a character vector
What's more, this error appears for all graphs, not just those produced by ggtern.
Here is an example.
library(ggplot2)
# This works
gg.mtcars <- ggplot(mtcars, aes(hp, mpg)) +
geom_point()
plotly::ggplotly(gg.mtcars)
library(ggtern)
data("Feldspar")
gg.feldspar <- ggtern(data = Feldspar, aes(x = Ab, y = An, z = Or)) +
geom_point()
# This throws an error
plotly::ggplotly(gg.feldspar)
# And now this throws an error too
plotly::ggplotly(gg.mtcars)

ggplot theme_set() only works intermittently

I'll often set a theme early on in a script, with a specific base font size.
# CODE BLOCK 1
library(tidyverse)
ggplot(mtcars, aes(cyl, mpg)) +
geom_col() +
theme_set(theme_bw(base_size = 20))
Then I'll want to switch the theme and the font size for a different plot later on in my script. I will try to accomplish this with code like below.
# CODE BLOCK 2
ggplot(mtcars, aes(factor(cyl))) +
geom_bar() +
theme_set(theme_gray(base_size = 6))
I just ran these two code blocks on a fresh R session and when I ran code block 2 the theme and base font size did not change to the arguments listed above. I immediately ran code block 2 again and the theme and font size changed to the arguments shown above. Why is theme_set() only working intermittently?
My session info follows:
OS: Windows 7 64 bit
R: 3.4.4
R Studio: 1.1.442
ggplot2: 2.2.1.9000
Please try this. See also here ?theme_set
theme_set(theme_bw(base_size = 20))
ggplot(mtcars, aes(cyl, mpg)) +
geom_col()
theme_set(theme_gray(base_size = 6))
ggplot(mtcars, aes(factor(cyl))) +
geom_bar()
Or simply without the theme_setfunction
ggplot(mtcars, aes(factor(cyl))) +
geom_bar() +
theme_gray(base_size = 6)

Error: StatBin requires a continuous x variable the x variable is discrete. Perhaps you want stat="count"?

I run this code yesterday and it worked however after running today, I got this error.
Error: StatBin requires a continuous x variable the x variable is discrete. Perhaps you want stat="count"?
library(rattle)
dsname <- "weatherAUS"
ds <- get(dsname)
p <- ggplot(ds, aes(Location, fill=Location))
p <- p + geom_bar(width=1, colour="white")
p <- p + theme(legend.position="none")
p <- p + coord_flip()
p <- p + geom_text(stat="bin",
color="white",
hjust=1.0,
size=3,
aes(y=..count.., label=..count..))
I was wondering how I can fix this problem.
Per my comment, it's never a good idea to update.packages or even install.package for new versions of things without checking what may potentially break. ggplot2 2.0 has some core differences that Hadley has been really good about communicating.
Here's a working version of the code:
library(rattle)
library(ggplot2)
data(weatherAUS)
p <- ggplot(weatherAUS, aes(Location, fill=Location))
# using geom_bar will automatically make a new "count" column
# available in an internal, transformed data frame. the help
# for geom_bar says as much
p <- p + geom_bar(width=1, colour="white")
# geom_text can then access this computed variable with
# ..count.. (I still thin that's horrible syntax, hadley :-)
p <- p + geom_text(aes(y=..count.., label=..count..),
stat="count", color="white",
hjust=1.0, size=3)
p <- p + theme(legend.position="none")
p <- p + coord_flip()
p
# to be more explicit to other readers of your code, you
# could also do this instead of the `geom_bar` call
p <- p + stat_count(width=1, colour="white", geom="bar")
Unless this is just for practicing ggplot2, coloring each of those bars uniquely is probably not a good idea for "production" visualizations.

ggplot2 rendering problems with gWidgets

There seems to be something odd going on with ggplot2 & gWidgets. See an example and the resulting image below.
Is there a way to make the facet labels render properly in gWidgets?
library(gWidgets)
library(ggplot2)
win <- gwindow("Graphics example")
ggraphics(cont = win)
p <- ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
facet_grid(. ~ cyl)
p

ggplot2 not showing line in geom_point's legend

I want to create a scatter plot with regression line, while using size aesthetics for one of attribute. I realized that the legend now have overlaid symbol for fitted line and I want to remove that, keeping only the legend for size. How can I do that?
> library(ggplot2)
> ggplot(mtcars, aes(wt, mpg, size=cyl)) + geom_point()
This much gives this picture, which is good:
Now having smooth line on top, and then this blue "line" is what i want to get rid of, or at least make all thin like the one in the plot is.
> ggplot(mtcars, aes(wt, mpg, size=cyl)) + geom_point() + stat_smooth()
Thanks!
use legend=FALSE option
ggplot(mtcars, aes(wt, mpg, size=cyl)) + geom_point() + stat_smooth(legend = FALSE)
The most recent documentation {ggplot2} version 2.2.1 uses legend.show= NA
ggplot(mtcars, aes(wt, mpg, size=cyl)) + geom_point() + stat_smooth(show.legend = F)

Resources