0

I have a dataframe organized as shown below.

The 'year' column data is classified as numerical, but the 'country' column data is classified as categorical (will this be problematic?).

I would like to be able to line plot a specific country's sales over time.

df <- read.table(header=TRUE,text='
    Country   Sales   Year
    USA       956     2018
    USA       855     2017
    UK        635     2018
    UK        588     2017
')

If possible, I would like to create numerous plots in a single go. I am able to plot the sales data by year without filtering country using this code, but I used facet_wrap to isolate each plot by year. What I would really like to do is plot "year" on the x-axis and isolate each plot by country. Unfortunately, I do not have enough "karma" points to insert an image of the output picture.

ggplot(data = df,
         aes(x = Country,
             y = Sales,
             col = Year)) +
  geom_point() +
  facet_wrap(~Year) +
  theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())
M.Viking
  • 5,067
  • 4
  • 17
  • 33
  • Can you explain further your desired output? Do you want X axis to be Year, and Color to be Country.... or do you wish to have several output charts (Saved as PNG?), one for each country... or ? – M.Viking Feb 15 '21 at 23:36
  • 1
    `ggplot(data = dataframe, aes(x = Year, y = Sales, col = Year)) + geom_point() + facet_wrap(~Country)` is this what you were looking for? – Dave2e Feb 15 '21 at 23:50
  • @Dave2e - Hi, and thank you for your help. This did create the plot series I was looking for! However the scale for my y axis between various countries are very different. Do you know of a function that would allow me to select a specific country, say if I only wanted to output USA data through time? Without luck, I tried modifying your code to `df %>% ggplot(df$Country=='USA', aes(x=Year, y=Sales)) + geom_point()` – Vivid_Angle1995 Feb 16 '21 at 15:56
  • To select a particular country this would work: `data = df[df$country =="USA",] %>% ggplot(aes(x=Year, y=Sales))` – Dave2e Feb 16 '21 at 23:08
  • Thank you, Dave2e! – Vivid_Angle1995 Feb 18 '21 at 20:10

2 Answers2

1

@Vivid

Try to use code bellow:

df1  %>%
  ggplot(aes(x=Year, y=Sales)) + 
  geom_line() +
  facet_grid(. ~ Country)
Rutangaba
  • 399
  • 3
  • 7
0

you're best off using a for-loop with the function print to plot your graphs


#get colnames of dataframe (df) to make a list of variable names to loop over.
colNames <- names(df)

#Variable you want on your x axis
X <- df$Year

#then for every variable in your list, print the desired plot
for(i in colNames){
  plt <- ggplot(df, aes_string(x= X, y=i)) +
geom_point() + theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())
  print(plt)
  Sys.sleep(2)
}

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mari
  • 47
  • 4
  • https://stackoverflow.com/questions/26034177/save-multiple-ggplots-using-a-for-loop This post has more detail on loops and plots too which I found helpful – Mari Feb 15 '21 at 23:50