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?

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
silent_hunter
  • 2,224
  • 1
  • 12
  • 30
  • 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`? – Rui Barradas Jan 02 '22 at 15:58
  • I want to have different colors for each value for gross_i.2019 and Prediction. – silent_hunter Jan 02 '22 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? – camille Jan 02 '22 at 17:52

2 Answers2

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

Andrew Ingalls
  • 117
  • 1
  • 8
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

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66