Aesthetics must either be length one, or the same length as the data
Ben's comment got me thinking about variable names, and I realized there were no specified variables in outliers (the original data frame, ratios3, and outliers both have many variables). I simply needed to specify the same variables:
geom_point(data = outliers, aes(`CPI-U 2015 (Official)`, `PCE1 2015`),
colour = "red", shape=1) +
R - Aesthetics must be either length 1 or the same as the data
If you subset anything, subset everything.
ggplot(data = elect[26:43,], aes(x = year, y = austria_rad_right)) +
geom_line() +
xlab("Year") +
ylab("Radical Right Vote Share") +
ggtitle("Austria Radical Right Support Over Time") +
theme_gray() +
theme(panel.grid = element_blank())
What you're doing with your code of aes(x = austria_year, y = austria_rad_right)
is similar to plot(21:23, 1:10)
(which fails). Okay, so the first few points are 21,1
, 22,2
, and 23,3
, but what do ,4
, ,5
, and beyond pair with?
Layer-specific data subset
Occasionally there is a need to show different portions of the data in different layers. For instance, points from all data and lines from a subset. For that, we can take advantage of data=
arguments to individual layers.
ggplot(mtcars, aes(mpg, cyl)) +
geom_point() +
geom_path(data = ~ .[with(., ave(cyl, cyl, FUN = seq_along) < 2),],
color = "red")
I'm using the rlang
-style ~
tilde function, where the .
is replaced by the data provided in the main ggplot(.)
call. Why is this useful? Because often one can change the data in the first line and forget to update it in the remaining data-dependent layers; in this fashion, the subset of data used for the layer is dynamic.
This would apply to your case if you intend to show a layer from all data and then only a subset of it for another layer.
Perhaps this:
ggplot(data = elect, aes(x = year, y = austria_rad_right)) +
geom_point(color = "gray") +
geom_line(data = ~ .[26:43,]) +
xlab("Year") +
ylab("Radical Right Vote Share") +
ggtitle("Austria Radical Right Support Over Time") +
theme_gray() +
theme(panel.grid = element_blank())
Admittedly I'd prefer a better method of subsetting the data, perhaps something like
geom_line(data = ~ subset(., country == "Austria")) +
(R) Error: Aesthetics must be either length 1 or the same as the data (6): x and y
library(gganimate)
# add an id row so that gganimate knows which line in the first
# frame matches the ones in the 2nd
Lines_frame = rbind(LINES, LINES_1) %>%
group_by(ind) %>% mutate(id = row_number()) %>% ungroup()
ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point() +
geom_segment(aes(x=Startx,y=Starty,xend=Endx,yend=Endy, group = id),
data=Lines_frame)+
coord_cartesian(ylim=c(9,36),xlim = c(1.5,6.2)) +
transition_reveal(ind)
There are a few transition options. For instance, you could alternate back and forth between the two states with transition_states
and make the movement tween with a bounce, because why not:
# wrapping in `animate()` to access its parameters, like frames per second (fps)
animate(
ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point()+
geom_segment(aes(x=Startx,y=Starty,xend=Endx,yend=Endy),
data=Lines_frame)+
coord_cartesian(ylim=c(9,36),xlim = c(1.5,6.2)) +
transition_states(ind, state_length = 0.2) +
ease_aes('bounce-in'),
fps = 20, nframes = 200, height = 400, width = 600)
Aesthetics must either be length one, or the same length as the dataProblems
The problem is that skew
isn't being subsetted in colour=factor(skew)
, so it's the wrong length. Since subset(skew, product == 'p1')
is the same as subset(skew, product == 'p3')
, in this case it doesn't matter which subset is used. So you can solve your problem with:
p1 <- ggplot(df, aes(x=subset(price, product=='p1'),
y=subset(price, product=='p3'),
colour=factor(subset(skew, product == 'p1')))) +
geom_point(size=2, shape=19)
Note that most R users would write this as the more concise:
p1 <- ggplot(df, aes(x=price[product=='p1'],
y=price[product=='p3'],
colour=factor(skew[product == 'p1']))) +
geom_point(size=2, shape=19)
ggplot: Error: Aesthetics must be either length 1 or the same as the data (10): x, y, group
Your data is of length 10, while your y aesthetic in geom_line
is of length 11.
seq(0, 1 , by = 0.1)
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
Try seq(0,0.9,by=0.1)
or seq(0.1,1,by=0.1)
Error: Aesthetics must be either length 1 or the same as the data (121): yintercept
The issue is that you map c(sig, -1*sig)
on yintercept
which will not work as the length of c(sig, -1*sig)
is two times the length of your df gacf.df
. That's what the error message is telling you. There are two options to achieve your desired result:
If you add
sig
as a variable you have to add the horizontal lines via two calls ofgeom_hline
.The approach below instead makes
sig
a scalar. In that case you don't have to wrapyintercept = c(sig, -1*sig)
insideaes()
:
correlogram <- function(x, type = "correlation"){
gacf = acf(x, plot=FALSE, lag.max=120, type = type)
gacf.df = with(gacf, data.frame(lag, acf))
#gacf.df$sig = qnorm((1 + 0.95)/2)/sqrt(length(x))
sig = qnorm((1 + 0.95)/2)/sqrt(length(x))
q <- ggplot(data = gacf.df, mapping = aes(x = lag, y = acf))
q <- q + xlim(c(0,120)) + theme_bw()
q <- q + geom_hline(aes(yintercept = 0))
q <- q + geom_segment(mapping = aes(xend = lag), yend = 0, lwd = 1)
# q <- q + geom_hline(aes(yintercept = sig), linetype = 2, colour = "#e51843")
# q <- q + geom_hline(aes(yintercept = -1*sig), linetype = 2, colour = "#e51843")
q <- q + geom_hline(yintercept = c(sig, -1*sig), linetype = 2, colour = "#e51843")
if(type == "partial"){
q <- q + ylab(expression(alpha[k]))
} else {
q <- q + ylab(expression(rho[k]))
}
q <- q + xlab("lag k")
}
library(gridExtra)
library(ggplot2)
library(grid)
set.seed(42)
prp <- data.frame(Log.prp.Standardized = rnorm(100))
q1 <- correlogram(prp$Log.prp.Standardized) + xlab(" ") + ggtitle("Total and Partial Correlograms")
q2 <- correlogram(prp$Log.prp.Standardized, type = "partial")
grid.arrange (q1, q2, nrow = 2)
Created on 2021-02-18 by the reprex package (v1.0.0)
Related Topics
How to Remove Empty Data Frames from a List
How Can R Loop Over Data Frames
R Draw All Axis Labels (Prevent Some from Being Skipped)
Ggplot Year by Year Comparison
Replacing All Occurrences of a Pattern in a String
Given a Set of Random Numbers Drawn from a Continuous Univariate Distribution, Find the Distribution
How to Insert (Add) a Row to a SQLite Db Table Using Dplyr Package
Plotting Normal Curve Over Histogram Using Ggplot2: Code Produces Straight Line at 0
Rescaling the Y Axis in Bar Plot Causes Bars to Disappear:R Ggplot2
Function for Retrieving Own Ip Address from Within R
R Markdown: How to Make Text Float Around Figures
Linear Model and Dplyr - a Better Solution
Names of R's Available Packages
Create Plots Based on Radio Button Selection R Shiny
Scale/Normalize Columns by Group
Can't Change Fonts in Ggplot/Geom_Text
Create Tables with Conditional Formatting with Rmarkdown + Knitr