Home > Net >  Scatter plot with ggplot2
Scatter plot with ggplot2

Time:01-03

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?

CodePudding user response:

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 enter image description here

CodePudding user response:

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

  • Related