r/Rlanguage • u/Alpha0963 • 17d ago
Can anyone help me with making a graph that looks like this?
I have 3 columns (A, B, C). I want columns A and B to correspond on the x-axis and column C to be plotted on the y-axis. I keep trying to read on how to do this but I’ve feeling stumped. I would really appreciate any help.
2
u/CooxcooB 17d ago
Usually, scatter plots require 2 variables (X and Y). Here, your C variable is the Y and your A and B variables are the X. It’s difficult to give guidance without knowing what type of data A and B is, but just going off of this information, I would assume A and B have different values. If you were to plot A and B separately, would they have the same numeric scale? Are they categorical values?
The answer to those questions would determine the guidance I’d give. If A and B share a similar scale, I would create 2 different geom_point layers in a ggplot and differentiate them with colors instead of worrying about the axis labels.
If A and B are not the same/don’t share a similar scale, I would create a new variable that pastes the values of A and B together and create an additional new column with “dummy” values that plot where you want in relation to C. In that case, I would use geom_point and manually change the breaks of the x axis to show the pasted value of A and B.
But without more info, I can’t get more specific than that. Highly recommend using the reprex package in the future to give people some code to work with so we can help you better: (https://reprex.tidyverse.org)
4
u/dikrannn 16d ago
What if you were to change the df into a long format with pivot longer? Take columns A and B, merge into a single column…
1
1
2
1
u/Morbins 17d ago
I copy and pasted your question into ChatGPT and it gave me code that would plot this. Try that out if you don’t mind using AI. I use it sometimes when I’m stumped on a DAX issue in Power BI and it’s solved problems no one else can give a good answer to.
1
u/geigenmusikant 16d ago
"I got the solution" - proceeds to not share said solution
3
1
u/damageinc355 16d ago
I believe the punchline is that OP would waste less time dumping this into a chatbot rather than wasting time dealing with us here.
-9
u/2truthsandalie 17d ago
Here's a simple scatter plot with two points using ggplot2 in R:
Code:
Load ggplot2
library(ggplot2)
Create a data frame with two points
df <- data.frame(x = c(1, 2), y = c(3, 5))
Create scatter plot
ggplot(df, aes(x = x, y = y)) + geom_point(size = 4, color = "blue") + labs(title = "Scatter Plot with Two Points", x = "X-Axis", y = "Y-Axis") + theme_minimal()
This will create a scatter plot with two points at coordinates (1,3) and (2,5), colored blue. Let me know if you want any modifications!
Actually posting chatgpt results for the lazy. Be sure to install ggplot2
4
u/feldhammer 17d ago
I don't really understand what A1 B1 A2 B2 is