Advertisement
vvccs

EXP9_BarGraph

Apr 21st, 2025
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.94 KB | None | 0 0
  1. Download R https://mirror.niser.ac.in/cran/bin/windows/base/R-4.5.0-win.exe
  2.  
  3. #Simple Bar Chart
  4. # Create the data for the chart
  5. A <- c(17, 32, 8, 53, 1)
  6.  
  7. # Plot the bar chart
  8. barplot(A, xlab = "X-axis", ylab = "Y-axis", main ="Bar-Chart")
  9.  
  10. #############################################################################
  11.  
  12. #Horizontal Bar Chart
  13. # Create the data for the chart
  14. A <- c(17, 32, 8, 53, 1)
  15. # Plot the bar chart
  16. barplot(A, horiz = TRUE, xlab = "X-axis",
  17. ylab = "Y-axis", main ="Bar-Chart")
  18.  
  19. #############################################################################
  20.  
  21. #Adding Color and Title
  22. # Create the data for the chart
  23. A <- c(17, 2, 8, 13, 1, 22)
  24. B <- c("Jan", "feb", "Mar", "Apr", "May", "Jun")
  25. # Plot the bar chart
  26. barplot(A, names.arg = B, xlab ="Month",
  27. ylab ="Articles", col ="green",
  28. main ="GeeksforGeeks-Article chart")
  29.  
  30. #############################################################################
  31.  
  32. #Creating Stacked Bar Chart
  33.  
  34. colors = c("green", "orange", "brown")
  35. months <- c("Mar", "Apr", "May", "Jun", "Jul")
  36. regions <- c("East", "West", "North")
  37. # Create the matrix of the values.
  38. Values <- matrix(c(2, 9, 3, 11, 9, 4, 8, 7, 3, 12, 5, 2, 8, 10, 11),
  39. nrow = 3, ncol = 5, byrow = TRUE)
  40. # Create the bar chart
  41. barplot(Values, main = "Total Revenue", names.arg = months,
  42. xlab = "Month", ylab = "Revenue",
  43. col = colors, beside = TRUE)
  44. # Add the legend to the chart
  45. legend("topleft", regions, cex = 0.7, fill = colors)
  46.  
  47. #Example 2
  48. colors = c("green", "orange", "brown")
  49. months <- c("Mar", "Apr", "May", "Jun", "Jul")
  50. regions <- c("East", "West", "North")
  51. # Create the matrix of the values.
  52. Values <- matrix(c(2, 9, 3, 11, 9, 4, 8, 7, 3, 12, 5, 2, 8, 10, 11),
  53. nrow = 3, ncol = 5, byrow = TRUE)
  54. # Create the bar chart
  55. barplot(Values, main = "Total Revenue", names.arg = months,xlab = "Month", ylab = "Revenue", col = colors)
  56. # Add the legend to the chart
  57. legend("topleft", regions, cex = 0.7, fill = colors)
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement