How can I understand if variable should be inside the aes or outside aes.

Variable inside the aes and outside has no effects for my plot. How can I expect something to understand this.

geom_vline(aes(xintercept = date), data = who_events, linetype = "dashed") +
  geom_text(aes(date, label = "event"), y = 100000, data = who_events)

This verse this

geom_vline(aes(xintercept = date), data = who_events, linetype = "dashed") +
  geom_text(aes(date),  label = "event", y = 100000, data = who_events)

or anyother example. with same issue.. Please provide any materials/

Without seeing the plots or data, I can't tell what your problem is. In general, assignments inside of the aes() cause the value of the parameter to change according to the data and assignments outside of the aes() are used for a constant value. I hope this example will be helpful.

DF <- data.frame( Xval = c(1,2,3),
                  Yval = c(2,2,3))

library(ggplot2)
#label is constant, its x an y value change
ggplot(data = DF, aes(x = Xval, y = Yval)) + 
  geom_text(label = "Const")

#label changes according to the Yval column
ggplot(data = DF, aes(x = Xval, y = Yval)) + 
  geom_text(aes(label = Yval))


#label changes with Yval but y is constant
ggplot(data = DF, aes(x = Xval, y = Yval)) +
  geom_point() +
  geom_text(aes(label = Yval), y = 2.5)

Created on 2022-10-09 with reprex v2.0.2

1 Like

In the last example If I move the y to the mapping and try to plot I see no difference.

ggplot(data = DF, aes(x = Xval, y = Yval)) +
  geom_point() +
  geom_text(aes(label = Yval, y = 2.5))

This and the last example of yours gives same plot. Atleast I can't see any difference.

01ec1a52-6bf8-478d-86f7-47f8dd5cf4f0

Then why would I write it outside?

It is true that both methods give the same result in that case. It is often true that there is more than one way to get a result. Usually, you use data to set values within the aes() but it is possible to do otherwise.

Well then my understanding is back at square 1. Although I learned something. Thanks so much :heart: .

The usual use of the aes() is to map values from the data to features in the plot. You can use it, at lease sometimes, to set fixed values for plot features. Can you explain more about what is confusing you?

ggplot(confirmed_cases_china_vs_world) +
  geom_line(aes(date, cum_cases, color = is_china)) +
  ylab("Cumulative confirmed cases") +
geom_vline(aes(xintercept = date), data = who_events, linetype = "dashed") +
  geom_text(aes(date), label = "event" , y = 100000, data = who_events)

I was studying on datacamp and I had put the label outside aes in the last line.
Which gave me this.

But my answer was wrong and the question wants me to put the label inside the aes.
Which gives this. i.e.

ggplot(confirmed_cases_china_vs_world) +
  geom_line(aes(date, cum_cases, color = is_china)) +
  ylab("Cumulative confirmed cases") +
geom_vline(aes(xintercept = date), data = who_events, linetype = "dashed") +
  geom_text(aes(date, label = "event"), y = 100000, data = who_events)

I could find any difference in the plot so I was thinking why this happened and how to know when to put variable inside the aes and when to outside, because both gives the same results.

If the intent of the plotting process is to place the word "event" in those three locations, then I agree that either answer works and marking your answer as wrong is incorrect. If the data set has a column named event and the intent is to place the content of that column on the plot, then you need to put event inside the aes and not put it in quotes.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.