I have a sample dataframe in R.
df <- data.frame(product_id= c("A","A","A","B","B","B","C","C"), year = c("2019", "2020", "2021", "2019", "2020", "2021", "2019", "2020"), profit=c(1.5, 2.2, 2.3, 1.4, 1, 16, 1.9, 25), sale = c(1.2, 1.8, 1.9, 2.0, 1.6, 20.0, 3.0, 33.2))
I am trying to draw three separate graphs in ggplot where y='year', x='profit' and data will be filtered by product_id. All the geom_point will be circular shape. This can be achieved by following code for Product A:
library(ggplot2)
library(dplyr)
ggplot(df) +
geom_point(data= df %>% filter(product_id =='A'), aes(x=year, y=profit, size=sale), shape = 16, color='Red') +
scale_size_continuous(range = c(5, 10), guide = FALSE) +
labs(x = "Year", y = "Profit")
But for customization, I am willing to change the fill-color of the circle if the y-axis value (profit) is less than 2.0. The border should be Red as the original color but the fill-color should be yellow. For the above graph, the point at 2019 will be red border and yellow filled color. I have tried that following way but it's not working:
library(ggplot2)
library(dplyr)
ggplot(test_df) +
geom_point(data= test_df %>% filter(product_id =='A'), aes(x=year, y=profit, size=sale), shape = 16, color='Red') +
scale_size_continuous(range = c(5, 10), guide = FALSE) +
scale_colour_manual(name = 'profit < 2', values = setNames(c('Yellow','Red'),c(T, F)), guide = FALSE) +
labs(x = "Year", y = "Profit")
Another problem is for all three graphs the size of the circular shapes aren't maintaining a standard relative size. I am trying to develop a standard size scale like
for 'sale' column value <=1.9; size will be 5,
for 1.9 < sale_value <= 10; size range will be 10 to 20,
for 10 < sale_value <= 20; size will be 25
and for sale_value > 20; size will be 30.
I can't figure out how this can be implemented or even it's possible or not.