-4

I'm thinking market share on y-axis, time on x-axis, and need to show my 5 different locations at each time. I have the data, just can't get a ggplot command to show me the locations all up on the same graph.

enter image description here

So now I have 8 data frames like this one. The market share is in Nokia phones. How do I graph the changing market share per in each region over time?

Joe Johnson
  • 73
  • 1
  • 1
  • 10
  • 1
    Must be a bug in the ggplot code. Would you like help finding it? Some advice [here](https://stackoverflow.com/help/mcve) and [here](https://stackoverflow.com/q/5963269/903061). – Gregor Thomas Aug 31 '17 at 17:09
  • In the future, yes, try to add some sample data to help ensure the quickest and most accurate answer. – www Aug 31 '17 at 17:28
  • Thanks @RyanRunge - added sample code – Joe Johnson Aug 31 '17 at 17:41
  • You're welcome. Doing that helped a lot to clarify your question. I've edited my answer accordingly. Please select my answer if this is now solved so that others with a similar question in the future will more quickly find the answer. – www Aug 31 '17 at 18:03
  • @RyanRunge I added the code and resulting graph. Only one country is showing. The "msnokia07, 08..." are the values I want graphed, which is why I replaced them – Joe Johnson Aug 31 '17 at 18:33
  • Okay, we're getting a lot closer; that's good. Would you be able to copy and paste the section of your code that applies to the data your working straight into the question text above? That way I can work directly with what you're using, and I know I can answer this for you if I have the right information. – www Aug 31 '17 at 18:38
  • @RyanRunge the section regarding the msnokia07,08...? The output for each year is [1] 0.33938802 0.34001877 0.31953275 0.30041254 0.26906012 0.16935548 0.12699261 0.08860855 – Joe Johnson Aug 31 '17 at 18:46
  • It's best to use a function like "dput(nameOfYourData)", and then copy/paste the output of that into your question above. This creates a reproducible structure of your data we can work from. See [this link](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) about stack overflow etiquette. Doing this helps people answer your question by being able to copy/paste that output into their own software and work from there to get you a quick answer. – www Aug 31 '17 at 19:00
  • @RyanRunge Ok, I changed some stuff up. I'm going to edit the post to show one data.frame I created. This will show the market share of nokia phones per region in 2007. I will create one for 2008-2014 as well. How can we use these 8 data.frames to visualize the change in market share per region over time? – Joe Johnson Aug 31 '17 at 19:41

1 Answers1

2

Try this:

require(ggplot2)
require(scales)

df <- data.frame(
  Location=c(paste0("Location_",c(sapply(1:5,function(x) rep(x,2))))),
  Market_Share=c(.10,.30,.30,.05,.20,.20,.15,.40,.25,.05),
  Date=c("2017-08-30","2017-08-31","2017-08-30","2017-08-31",
         "2017-08-30","2017-08-31","2017-08-30","2017-08-31",
         "2017-08-30","2017-08-31"),
  stringsAsFactors = FALSE
)

ggplot(df,aes(x=as.Date(Date),y=Market_Share,color=Location)) +
  geom_point() +
  geom_line() +
  scale_y_continuous(labels = percent_format()) +
  scale_x_date(labels = date_format("%b-%d-%Y")) +
  xlab("Date") +
  ylab("Market Share")

Output:

enter image description here

Edit:

Given your clarification of the question, something like this might be more of what your looking for:

df <- data.frame(
  Location=c("Argentina","Argentina","Brazil","Brazil",
             "Peru","Peru","Venezuela","Venezuela",
             "Chile","Chile"),
  Units=c(7612.40,0.00,540.90,2139.10,879.70,5796.10,
          25.90,760.00, 2615.70, 1386.30),
  Date=c(rep(c("2007","2008"),5)),
  stringsAsFactors = FALSE
)

ggplot(df,aes(x=as.numeric(Date),y=as.numeric(Units),
              color=Location)) +
  geom_point() +
  geom_line() +
  scale_x_continuous(breaks=as.numeric(df$Date)) +
  xlab("Date") +
  ylab("Units")

New output:

enter image description here

Community
  • 1
  • 1
www
  • 4,124
  • 1
  • 11
  • 22
  • Ok to keep clarifying, I have dates ranging from 2007-2014, and 46 total countries. Is there an easier way to graph this without having to type in all 46 countries, 8 times for each year? – Joe Johnson Aug 31 '17 at 18:10
  • or maybe a better plot to do this? – Joe Johnson Aug 31 '17 at 18:12
  • my bad, disregard the "46 countries" part. I need to graph how the market share changes from 2007-2014 across the 7 total regions only – Joe Johnson Aug 31 '17 at 18:19
  • Is your data in a text file? A csv or excel file or something? What I've provided is just a demo of the concepts. For your actual data, you don't need to type in everything manually if it's already in a file. Just read in your data with something like: "df <- read.csv('yourData.csv')". – www Aug 31 '17 at 18:25
  • Yeah, I've gotten that far. I used your code and got a graph but of only one region, I will add pic of my code and graph – Joe Johnson Aug 31 '17 at 18:29