简体   繁体   中英

Converting scatter point to single mean line plot

I have a graph scattered plot drawn with code below. But I would like to draw a single line of the mean of each Industry. Converting geom_point to geom_line does not give single line but gives a density like graph.

  output$ind=renderPlot({
     ggplot(data = dailyprice_gather[dailyprice_gather$Industry == input$industry2,]) +
     geom_point(aes(x= Date , y= cumulative, color=Industry) , size=0.25) +
     ggtitle(paste0("Simple Cumulative Return over Years - Industry Wise"))

     })

Sample of my data set is :

structure(list(Date = structure(c(17833, 17830, 17829, 17828, 
NA), class = "Date"), stocks = structure(c(1L, 1L, 1L, 1L, 1L
), .Label = c("DBS SP Equity", "OCBC SP Equity", "ST SP Equity"
), class = "factor"), cumulative = c(22.99, 23.1, 23.71, 24.1, 
NA), Industry = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("Banks", 
"Telecommunications"), class = "factor")), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))

这是当前的geom_point

I would like to plot 1 single average line instead of 3 as shown now.so basically plotting average(3 stocks in a Bank industry. Other industry could have more or less number of stocks within them

Is this what your looking for? Because your dataset is not complete I used the iris dataset. I first calculate the mean of what you to show and add this as an extra column to the data frame. Then this column is easy to use in ggplot .

library(tidyverse)

data(iris)

iris <- iris %>% 
  group_by(Species) %>% 
  # calculate the mean of what your plotting on the y-axis
  mutate(Petal.Width.mean  = mean(Petal.Width))

iris %>% 
  ggplot(aes(x = Petal.Length, 
             y = Petal.Width, 
             color = Species)) +
  geom_point() +
  geom_line(aes(x = Petal.Length,
                y = Petal.Width.mean))

does this help?

+geom_smooth(method = lm, se = FALSE)

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