1

I am trying to make scatter plot with ggplot2. Below you can see data and my code.

 data=data.frame(
      gross_i.2019=seq(1,101),
      Prediction=seq(21,121))
   
   
   ggplot(data=data, aes(x=gross_i.2019, y=Prediction, group=1)) +
      geom_point()
   

This code produce chart below

enter image description here

So now I want to have values on scatter plot with different two different colors, first for gross_i.2019 and second for Prediction. I try with this code below with different color but this code this lines of code only change previous color into new color.

sccater <- ggplot(data=data, aes(x=gross_i.2019, y=Prediction))
sccater + geom_point(color = "#00AFBB")

enter image description here

So can anybody help me how to make this plot with two different color (e.g black and red) one for gross_i.2019 and second for Prediction?

3
  • I don't understand how can you have two colours if you only have one line. Do you want a line for gross_i.2019 and another one for Prediction? Commented Jan 2, 2022 at 15:58
  • I want to have different colors for each value for gross_i.2019 and Prediction. Commented Jan 2, 2022 at 16:00
  • The coordinates of each point are (gross, prediction), so how would the colors be different for the two values? Are you sure that what you want is a scatterplot? Commented Jan 2, 2022 at 17:52

2 Answers 2

2

I may be confused by what you are trying to accomplish, but it doesn't seem like you have two groups of data to plot two different colors for. You have one dependent(Prediction) and one independent (gross_i.2019) variable that you are plotting a relationship for. If Prediction and gross_i.2019 are both supposed to be dependent variables of different groups, you need a common independent variable to plot them separately, against (like time for example). Then you can do something like geompoint(color=groups)

Edit1: If you wanted the index (count of the dataset to be your independent x axis then you could do the following:

library(tidyverse)

data=data.frame(gross_i.2019=seq(1,101),Prediction=seq(21,121))


#create a column for the index numbers
data$index <- c(1:101)

#using tidyr pivot your dataset to a tidy dataset (long not wide)
data <- data %>% pivot_longer(!index, names_to="group",values_to="count")

#asign the groups to colors
p<- ggplot(data=data, aes(x=index, y=count, color=group))
p1<- p + geom_point()
p1

plot

Sign up to request clarification or add additional context in comments.

Comments

1

This type of problems generally has to do with reshaping the data. The format should be the long format and the data is in wide format. See this post on how to reshape the data from wide to long format.

long <- reshape(data, 
                ids = row.names(data),
                varying = c("gross_i.2019", "Prediction"),
                v.names = "line",
                direction = "long")
long$time <- names(data)[long$time]
long$id <- as.numeric(long$id)

library(ggplot2)
ggplot(long, aes(id, line, color = time)) +
  geom_point() +
  scale_color_manual(values = c("#000000", "#00AFBB"))

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.