Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)
How can I build a line chart for the 'WorldPhones' dataset in R? The dataset is of class - "matrix" "array". I need to plot a line chart for the number of telephones in North America, Asia, and Europe between 1956-1961.

1 Answer

0 votes
by (107k points)

In R programming, the instance on the help page for the dataset provides a plot using matplot. For something slightly more pleasing, you can try ggplot.

library(tidyr)   # For pivoting the data into long-form 

library(tibble)  # For converting the rownames (Year) to a column

library(scales)  # For scaling the y-axis and labels

library(ggplot2) # For the plot

WorldPhones %>%

  as.data.frame() %>%

  rownames_to_column("Year") %>%

  pivot_longer(cols=-Year, names_to="Country", values_to="Users") %>%

  ggplot(aes(Year, Users, group=Country, col=Country)) +

  geom_line() +

  scale_y_log10(n.breaks=5, labels = trans_format("log10", math_format(10^.x))) +

  theme_minimal() 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...