ggplot2 error no layers in plot
The error message is due to fact that you didn't save d+geom_line()
as an object.
#Save ggplot() as object
d <- ggplot(data=data3, aes(x=MonthNB, y=Ptot, colour=StationNAME))
#Add to d geom_line() - this makes the plot to appear on the screen but not saved.
d + geom_line()
To save layer to object
d<-d+geom_line()
#No error message
d
No layers in plot (R)
Just a simple example to see how to use the commands:
library(ggplot2)
dt = data.frame(Chr = c("c1","c1","c1","c2","c2","c2","c3","c3","c3"),
x = c(1,2,3,4,5,6,7,8,9),
y = c(2,4,5,2,3,4,6,6,7))
ggplot(dt, aes(x,y, col=Chr)) +
geom_point(size = 3) +
geom_line() +
facet_grid(. ~ Chr) # remove this to have all lines in same plot
Error: No layers in plot when using ggplot
Since you have presummarised data, you need to specify stat = "identity"
in geom_bar
.
library(ggplot2)
ggplot(AD0, aes(group, mean)) + geom_bar(stat = "identity")
Furthermore, I suppose you want to use group
for the x-axis and mean
for the y-axis. I switched the order of both names.
R ggplot2 facetting - Error: No Layers in Plot
You had everything there, just an error in your assignment to m
. Either of these should do the trick:
m <- ggplot(mydata, aes(x=T_MEAN))
m + geom_histogram(aes(y = ..density..)) + geom_density() + facet_grid(~ POSCAT)
or
m <- ggplot(mydata, aes(x=T_MEAN))
m <- m + geom_histogram(aes(y = ..density..)) + geom_density()
m + facet_grid(~ POSCAT)
ggplot overlay plot error, no layers in plot
You have to tell ggplot()
which points to connect by lines. This is done by adding group=variable
inside the aes()
.
ggplot(data = dat2,
aes(x = Locs, y = value, colour = variable,group=variable)) +
geom_line()
Check for NULL ggplot object before adding layers in dplyr pipe
Option 1: Blank plot
This doesn't directly answer your question, but how about a different approach? When the dataframe has zero rows, use geom_blank()
to create an empty plot. Adding a title later doesn't return an error.
plot_fun <- function(df) {
if (nrow(df) > 0) {
df %>%
ggplot(., aes(Sepal.Length, Sepal.Width)) +
geom_point()
} else {
ggplot(data = data.frame()) +
geom_blank()
}
}
iris %>%
filter(Sepal.Length > 1000) %>%
plot_fun() +
ggtitle("Plot me")
Option 2: Pass layers into a function
You can pass layers as function arguments. Here's a function that checks whether the plot exists, and then adds the layers only if it does.
append_layers_maybe <- function(p, l) {
if(!is.null(p)) {
p + l
}
}
iris %>%
plot_fun() %>%
append_layers_maybe(facet_wrap(~ Species)) %>%
append_layers_maybe(ggtitle("Foo"))
iris %>%
filter(Sepal.Length > 1000) %>%
plot_fun() %>%
append_layers_maybe(facet_wrap(~ Species)) %>%
append_layers_maybe(ggtitle("Plot me"))
ggplot object not found error when adding layer with different data
Aesthetic mapping defined in the initial ggplot
call will be inherited by all layers. Since you initialized your plot with color = GROUP
, ggplot
will look for a GROUP
column in subsequent layers and throw an error if it's not present. There are 3 good options to straighten this out:
Option 1: Set inherit.aes = F
in the layer the you do not want to inherit aesthetics. Most of the time this is the best choice.
ggplot(dummy,aes(x = X, y = Y, color = GROUP)) +
geom_point() +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2),
data = df,
inherit.aes = FALSE)
Option 2: Only specify aesthetics that you want to be inherited (or that you will overwrite) in the top call - set other aesthetics at the layer level:
ggplot(dummy,aes(x = X, y = Y)) +
geom_point(aes(color = GROUP)) +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2),
data = df)
Option 3: Specifically NULL
aesthetics on layers when they don't apply.
ggplot(dummy,aes(x = X, y = Y, color = GROUP)) +
geom_point() +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, color = NULL),
data = df)
Which to use?
Most of the time option 1 is just fine. It can be annoying, however, if you want some aesthetics to be inherited by a layer and you only want to modify one or two. Maybe you are adding some errorbars to a plot and using the same x
and color
column names in your main data and your errorbar data, but your errorbar data doesn't have a y
column. This is a good time to use Option 2 or Option 3 to avoid repeating the x
and color
mappings.)
Related Topics
Converting Date to a Day of Week in R
Insert Images Using Knitr::Include_Graphics in a for Loop
R: Adding Alpha Bags to a 2D or 3D Scatterplot
Export All User Inputs in a Shiny App to File and Load Them Later
How to Prevent Functions Polluting Global Namespace
Setting Ld_Library_Path from Inside R
Colons Equals Operator in R? New Syntax
Adding Regression Line Equation and R2 on Separate Lines Graph
How Do Add a Column in a Data Frame in R
Scraping from Aspx Website Using R
Cumulative Count of Unique Values in R
Use Href Infobox as Actionbutton
Object.Size() Reports Smaller Size Than .Rdata File
How Calculate Growth Rate in Long Format Data Frame
Plotting a 95% Confidence Interval for a Lm Object
R: Save Multiple Plots from a File List into a Single File (Png or PDF or Other Format)