0

I need to create a single graph that includes six time plots (year v budget), one for each of the following cities...

city1, city2, city3, city4, city5, and city6.

year (1860-1910) needs to be on the x-axis, and budget (ranging from -$100 to $800) on the y-axis.

I have this code to display the six graphs in the viewer panel:

par(mfrow=c(2,3))

I used this code to produce the graphs, written six times (one for each city)...

plot.default(mydata$budget[mydata$city=="city1"], 
mydata$year[mydata$city=="city1"], xlim=c(1860,1910), 
ylim=c(-100,800), xlab="City1", ylab="Budget")

However, the issue I'm running into is that the data are not appearing in the graphs produced. In other words, each time I execute the above code, I get a blank graph (with the correct x and y limits) in the viewer panel. How do I get the data to appear in the graph so that there is a dot for each year and a line connecting each dot to the subsequent one?

Here's some of the data...

   city   aid_sums year  budget        
 1 Boston        0 1860   1.7764366           
 2 Boston        0 1861   3.5162529  
 3 Boston        0 1862   1.6834916 
 4 Boston        0 1863   3.5736373  
 5 Boston        0 1864   4.4076780  
 6 Boston        0 1865   5.0411372  
 7 Boston        0 1866   4.6084619 
 8 Boston        0 1867   3.6368014 
 9 Boston        0 1868   2.5624348 
10 Boston        0 1869   2.3336493 
11 Boston        0 1870   2.3075819 
B.Nin
  • 41
  • 2
  • Also, the data is in long form. – B.Nin Nov 28 '21 at 19:58
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 28 '21 at 19:59
  • Thanks, I just provided a sample. – B.Nin Nov 28 '21 at 20:14

1 Answers1

0

In a plot statement the values have to be in the order X, then Y, or you need to specify them as "x = " and "y = ". NB: Made up values througout.

# Specify Y and X
plot.default(y = mydata$budget[mydata$city=="city1"], 
             x = mydata$year[mydata$city=="city1"], xlim=c(1860,1910), 
             ylim=c(-100,800), xlab="city1", ylab="Budget", type = "l")

# Make them the right order: X, then Y
plot.default(mydata$year[mydata$city=="city1"], 
             mydata$budget[mydata$city=="city1"], xlim=c(1860,1910), 
             ylim=c(-100,800), xlab="city1", ylab="Budget", type = "b")

Though, if your data is in a long format, then it might be easier to use ggplot() and facet_wrap to produce them all in one go.

Something like:

library(tidyverse)

ggplot(mydata) +
  geom_line(aes(x = year, y = budget, colour = city)) +
  geom_point(aes(x = year, y = budget, colour = city)) +
  scale_x_continuous(limits = c(1860, 1910), breaks = seq(from = 1860, to = 1910, 10)) +
  scale_y_continuous(limits = c(-100, 800)) +
  facet_wrap(~city)

enter image description here

Tech Commodities
  • 1,884
  • 6
  • 13