I have this data frame in R. eyecolor = tibble( eye = c("Pruun", "Pruun", "Sinine",…
- User
I have this data frame in R. eyecolor = tibble( eye = c("Pruun", "Pruun", "Sinine", "Sinine", "Helepruun", "Helepruun", "Roheline", "Roheline"), gender = c("Mees", "Naine", "Mees", "Naine", "Mees", "Naine", "Mees", "Naine"), count = c(98, 122, 101, 114, 47, 46, 33, 31) )
I need to visualise it with a barplot using only the grid package. X axis stands for count, Y axis for gender and color for eye.
- Assistant
Here's how you can visualize the data frame using a bar plot with the grid package:
library(grid) # Create a new plot with the specified dimensions grid.newpage() pushViewport(viewport(width = 0.8, height = 0.6)) # Set up the color palette colors <- c("Pruun" = "brown", "Sinine" = "blue", "Helepruun" = "tan", "Roheline" = "green") # Create the bar plot grid.rect(gp = gpar(fill = colors[eyecolor$eye]), width = eyecolor$count/5, height = 0.5) grid.text(eyecolor$count, x = eyecolor$count/5 + 1, y = 1:8, label = eyecolor$count, gp = gpar(fontsize = 10)) grid.text(eyecolor$gender, x = 0.5, y = 1:8, label = eyecolor$gender, gp = gpar(fontsize = 10, fontface = "bold")) grid.text(eyecolor$eye, x = 0.5, y = 1:8, label = eyecolor$eye, gp = gpar(fontsize = 10, col = "white")) # Add axes and labels grid.yaxis(at = 1:8, labels = eyecolor$gender, gp = gpar(fontsize = 10)) grid.xaxis(at = seq(0, max(eyecolor$count), by = 20), gp = gpar(fontsize = 10)) grid.text("Count", x = max(eyecolor$count), y = -1, gp = gpar(fontsize = 10)) # Add a title grid.text("Eye Color by Gender", x = 0.5, y = 8.5, gp = gpar(fontsize = 12, fontface = "bold")) # Clean up the viewport popViewport()