简体   繁体   中英

Scatter plot with ggplot2

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

在此处输入图像描述

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")

在此处输入图像描述

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

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"))

在此处输入图像描述

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

阴谋

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM