categorical ploting using ggplot2

Hi, I'm trying to plot a categorical variable, the code works if i plot all categories and separate them by color. this is fine but it is not what I want. Instead, i want to isolate the categories such that the plot only shows points of a single category. i have set my aesthetics individualy because i want to have various plots from the same data on a single canvas so that i can do comparison. the code gives me an error as shown below.

ggplot(data) + geom_point(aes(x= A[gender == "male"],y = B [gender == "male"])) +
geom_smooth(aes (x= A[gender == "female"],y = B [gender == "female"]), method= 'lm' , se = F)

Error: Must subset columns with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x Input has size 12 but subscript gender == "male" has size 1296.
Run rlang::last_error() to see where the error occurred

Hello and Welcome to the forum, I hope you will find us friendly and helpful.

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

say for example i'm using the iris data-set as you illustrated above. i want to plot the sepal.width vs sepal.length with respect to the species column. But instead of color coding each species on the plot, i want the plot to display only the data from the setosa species alone

for me to plot it by color it would be

ggplot(iris, aes(x=sepal.length, y= sepal.width), col= species )+ geom_point()

but in my case i want to draw a specific specie from the species column. what i have tried was:

ggplot(iris, aes(x=sepal.length[species=="setosa], y= sepal.width[species=="setosa]))+ geom_point()

after running the code, it produces the error:

Error: Must subset columns with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x Input has size 12 but subscript gender == "male" has size 1296.
Run rlang::last_error() to see where the error occurred

in my case,"gender" and "male" represents the variable and subset category similar to"species" and "setosa" in the iris dataframe

Hi, you can set an entire plot, or even just one geom to use a subset of the data, but you would do it like this, with a formula and on the data portion, reserve the aes for only assigning variables to roles.

ggplot(iris) + geom_point(data=~filter(.x,
                                       Species=='setosa'),
                          aes(y=Petal.Length,x=Species))
1 Like

thank you. I have applied the data=~filter (.x, function and the code is now running as intended. the correct function I applied was:

ggplot(iris) + geom_point(data=~filter(.x,Species=='setosa'),
aes(y=sepal.width,x=sepal.length))

If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).

Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.

Thanks

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.