Simple graph, but erratic. How?

Here is my data

structure(list(Year = c("1950-51", "1951-52", "1952-53", "1953-54", 
"1954-55", "1955-56", "1956-57", "1957-58", "1958-59", "1959-60", 
"1960-61", "1961-62", "1962-63", "1963-64", "1964-65", "1965-66", 
"1966-67", "1967-68", "1968-69", "1969-70", "1970-71", "1971-72", 
"1972-73", "1973-74", "1974-75", "1975-76", "1976-77", "1977-78", 
"1978-79", "1979-80"), `Public GFCF` = c(2.59126183036084, 2.80761434093375, 
3.04842470565375, 3.29339286230763, 4.14035191522251, 5.55749547124394, 
5.43321877737429, 5.50292924647046, 5.36324531458431, 6.5113470265288, 
6.89360010240323, 6.82315718785446, 7.57852642804014, 7.82788895799515, 
7.89049700644928, 8.36712198330739, 7.39379985141297, 6.20246017616733, 
6.12383224326271, 5.79057795304461, 5.87924388536997, 6.48516773083808, 
7.53992163024017, 6.87591493571425, 6.21840778434406, 7.50116499439853, 
8.72191143669707, 8.42057334086473, 8.52234367445929, 9.28672883727794
), `Private GFCF` = c(8.84506846746929, 8.75239433607971, 8.01602072370182, 
6.74974908136686, 8.27927792035339, 9.16929073903156, 9.9800610916, 
9.46965847703862, 8.10243211071489, 7.42493012518177, 7.56230655078099, 
8.93280005424918, 8.38786245669229, 8.5165262602481, 8.39579106949271, 
8.75299561571842, 9.33835397580938, 9.86634321712943, 10.0847546917336, 
10.3331267201426, 9.6569149928854, 10.1005327230576, 9.51251153641325, 
9.03137020562907, 10.3860202055142, 10.2135458446367, 9.76703639626767, 
10.3696192920359, 10.8701925609434, 11.067585184788), yr_num = c(1950, 
1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 
1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 
1973, 1974, 1975, 1976, 1977, 1978, 1979)), row.names = c(NA, 
-30L), class = c("tbl_df", "tbl", "data.frame"))
plot(GFCF$yr_num[31:65], GFCF$`Private GFCF`[31:65], type='l', col='red',xlab='Year', ylab='Private GFCF(%GDP)', main='Trends: Private and Public GFCF')
lines(GFCF$yr_num[31:65], GFCF$`Public GFCF`[31:65], type='l', col='blue')

The above code gives the following graph:

wrong1

As you can see the blue line does not extend till the end of timeline, even though it should, WHy?. Or maybe it does but there seems to be an axis problem. What do i do?

The limits of the y axis are set when you call plot(). You should set them manually after checking what the range of the two data sets is. Compare the two plots below.

DF <- data.frame(X = 1:5, Y1 = c(5,6,7,8,9), Y2 = c(7,6,5,4,3))
plot(DF$X, DF$Y1, type = "l")
lines(DF$X, DF$Y2, col = "red")


#Check the range of y values
range(c(DF$Y1,DF$Y2))
#> [1] 3 9

#set appropriate limits
plot(DF$X, DF$Y1, type = "l", ylim = c(3,9))
lines(DF$X, DF$Y2, col = "red")

Created on 2024-09-02 with reprex v2.0.2

1 Like

calculate the y limits from the range of both Public GFCF and Private GFCF:

plot(
  GFCF$yr_num, 
  GFCF$`Private GFCF`, 
  type='l', 
  col='red',
  xlab='Year', 
  ylab='Private GFCF(%GDP)', 
  xlim = range(GFCF$yr_num),
  ylim = range(c(GFCF$`Public GFCF`, GFCF$`Private GFCF`)),
  main='Trends: Private and Public GFCF')

lines(GFCF$yr_num, GFCF$`Public GFCF`, type='l', col='blue')
1 Like

You are the best, FJCC.

Wow I see, thanks mduvekot. May you have a good day.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.