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.)
Object not found in ggplot
I don't think you need the scale_x_continuous
, since (1) it's discrete, so (2) we can factor
the data.
ggplot(BOTTRP, aes(factor(Chatbot), Mean, colour = Scale))+
geom_point(size = 2)+
labs(y = "Mean of chatbot evaluation", size=3)+
theme_gray()+
expand_limits(x=1:10, y=1:5)
R: Object not found with ggplot
This is similar to the other answer but without needless dplyr mumbo-jumbo and pointing out some important details. The point is that if you want manual colors, you should use a manual color scale.
mycol = c("#A38FCC","#7F40FF")
mynam = c("random_line1", "random_line2")
set.seed(123)
df=data.frame(
#month needs to be an ordered factor to get correct order in the plot
#an unordered factor would be ordered alphabetically by ggplot2
month = ordered(month.abb, levels = month.abb),
mpft = c(rep(1,6),rep(2,6)),
ran = runif(12,0.,10.))
library(ggplot2)
ggplot(df, aes(x=month,y=ran,
colour = factor(mpft) #you want a discrete color scale
)) +
geom_line(aes(group = mpft), size = 1) +
scale_colour_manual(name = "mynam",
#always pass named character vectors here to ensure correct mapping
values = setNames(mycol, unique(df$mpft)),
labels = setNames(mynam, unique(df$mpft)))
ggplot geom_rect() error object not found
You just have an extra step of setting up a dataframe in geom_rect
which coincide with data in ggplot
. Simply provide your max and min values to geom_rect
and it works:
ggplot(df, aes(Month, Value, group = 1)) +
geom_line() +
theme_minimal() +
geom_rect(aes(xmin = min(as.integer(Month)) - 0.5,
xmax = max(as.integer(Month)) + 0.5,
ymin = min(Value),
ymax = max(Value)),
alpha = 0.2/nrow(df), fill = "green")
Related Topics
R Create Function to Add Water Year Column
Plot Line on Top of Stacked Bar Chart in Ggplot2
R: Save All Data.Frames in Workspace to Separate .Rdata Files
Let Each Plot in Facet_Grid Have Its Own Y-Axis Value
How to Take a Rolling Product Using Data.Table
Parsimonious Way to Add North Arrow and Scale Bar to Ggmap
Aggregating Values on a Data Tree with R
Plotting Multiple Lines from a Data Frame in R
Exporting Multiple Panels of Plots and Data to *.Png (In the Style Layout() Works Within R)
R Find the Distance Between Two Us Zipcode Columns
How to Write a Data-Frame with One Column a List to a File
Convert a File Encoding Using R? (Ansi to Utf-8)
Remove Multiple Patterns from Text Vector R
Build Word Co-Occurence Edge List in R
How Does Settimelimit Work in R
How to Tell Which Packages I am Not Using in My R Script
Using Data.Table to Create a Column of Regression Coefficients