-2

I'm trying to plot a data frame that has "Date" as the x-axis, and stock price as the y-axis, and I have four different stocks to be plotted. I'm very confused by the ggplot documentation, and haven't found an easy solution to this. Here is the data frame:

appleData <- read.csv("AAPL.csv", header = TRUE)
microsoftData <- read.csv("MSFT.csv", header = TRUE)
googleData <- read.csv("GOOG.csv", header = TRUE)
amazonData <- read.csv("AMZN.csv", header = TRUE)

names(appleData) <- c("Date", "AAPL")
names(microsoftData) <- c("Date", "MSFT")
names(googleData) <- c("Date", "GOOG")
names(amazonData) <- c("Date", "AMZN")

mergedData1 <- merge(appleData, microsoftData, by = "Date")
mergedData2 <- merge(googleData, amazonData, by = "Date")
totalData <- merge(mergedData1, mergedData2, by = "Date")
totalData

The dataframe is called "totalData", and when I use ggplot(totalData) I get a blank plot. What I need help with specifically is plotting all four stocks onto the same plot, and also rescaling the prices so that they all begin at $100 (so they are on the same scale). Thank you in advance.

user9170959
  • 35
  • 1
  • 5
  • I had added one example at https://stackoverflow.com/questions/48491246/chart-in-r-not-displaying-data/48491513#48491513 . It may help you. – MKR Jan 29 '18 at 22:34
  • Please post data as plain text, not images, so others can copy/paste it. – neilfws Jan 29 '18 at 22:37
  • I tried this following exactly as it said and the warning says "geom_path: Each group consists of only one observation" Help? – user9170959 Jan 29 '18 at 22:44

1 Answers1

1

I found your question a little difficult to help with because you didn't provide the data you are using. Check out this amazing reference on how to ask really good questions that get answered quickly! How to make a great R reproducible example?

I hope this below code helps you get started on answering your question.

One of the main things I did was I converted your data from an untidy "wide" dataframe to a "tidy" long dataframe using the gather function from tidyr. I highly recommend that you check out this excellent tutorial http://garrettgman.github.io/tidying/ that goes into the basics of tidying. Once your data is "tidy" you will find many tools will work much easier!

Good Luck!


library(dplyr)
library(tidyr)
library(ggplot2)

# create sample data frame with random numbers
set.seed(123)
total_data <- data.frame(date = seq.Date(from = as.Date("2018-01-01"), 
                                             to = as.Date("2018-01-31"), by = "day"),
                         AAPL = sample(100:1000, 31),
                         MSFT = sample(100:1000, 31),
                         GOOG = sample(100:1000, 31),
                         AMZN = sample(100:1000, 31))
head(total_data)
#>         date AAPL MSFT GOOG AMZN
#> 1 2018-01-01  359  912  445  691
#> 2 2018-01-02  809  721  346  388
#> 3 2018-01-03  467  815  832  268
#> 4 2018-01-04  892  122  502  802
#> 5 2018-01-05  943  528  826  183
#> 6 2018-01-06  140  779  827  518

# convert your wide data frame to a tidy long data frame
total_data <- gather(total_data, company, value, -date)

# plot using ggplot2
total_data %>%
  ggplot(aes(x = date, y = value, color = company)) +
  geom_line()
Greg B
  • 86
  • 4
  • I still keep getting the "Each group consists of only one observation" error... I printed my data "totalData" and it looks exactly like yours in the example. – user9170959 Jan 29 '18 at 23:56
  • @user9170959 can you post the code you are using to create the ggplot? – Greg B Jan 30 '18 at 00:05