0

I can't seem to get around this error where it says the aesthetics must either be length 1 or the same as the data, which in my case is 368.
I have created two variables from the data, both of which are 18 values. These are my x and y values, where the x axis is the year, and the y axis is the vote share of radical right candidates in those years. My variables are the same length, so why is this error occurring and how do I correct this?
Too new to include pics yet, so here's a link to what I'm seeing...

austria_year <- elect$year[26:43]

ggplot(data=elect, aes(x=austria_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())
Dave2e
  • 22,192
  • 18
  • 42
  • 50
redleg_64
  • 39
  • 1
  • 8
  • 3
    `data` has at least 43 rows, yes `austria_year` has many fewer. Either use `data=elect[26:43,]` or use `aes(x=austria_year)`. – r2evans Oct 05 '21 at 14:01
  • 2
    Please do not post photos of data or code! If you do, people who are willing to help you would have to type out all that text. Instead proved a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) P.S. Here is [a good overview on how to ask a good question](https://stackoverflow.com/help/how-to-ask) – dario Oct 05 '21 at 14:46
  • @dario, I provided the code in my post. It's plainly visible for everyone to copy and mess around with if need be. The picture just includes the the bit on my environment pane where the number of values I'm using can be verified. But thanks... – redleg_64 Oct 05 '21 at 20:29
  • @r2evans, thanks. I didn't realize I needed to index the dataset I was using. I thought that it would just pull values out of the columns that included Austria in the particular row. I'm new to this and still learning. I think I'm getting the hang of it, but it's the small things that trip me up and are easy to overlook. – redleg_64 Oct 05 '21 at 20:38
  • The picture is fine given that your sole purpose was to show your environment with the code. Since you provided real code (thank you!), the picture doesn't really add much. It does show us what the data *looks like* in a sense, but (1) it breaks screen-readers, vis-impaired readers need it too, so they do not "see" the glimpse of data; and (2) we don't have any sample data to actually attempt your real code; sometimes people will transcribe your data for you, many choose not to. Regardless, my first comment fixes the bug. – r2evans Oct 05 '21 at 21:35

1 Answers1

2

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")

ggplot with per-layer data

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")) +
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • This is helpful. I just saw this answer, but a few hours ago I went through everything and subsetted the data differently. It ended up being exactly what you described here. Trying to index specific rows caused me a lot of headaches, and with the subsetting you describe here that allowed me to rewrite the code without the indexing. – redleg_64 Oct 06 '21 at 02:54