Home > Enterprise >  Bar Graph, graphing Integers as strings
Bar Graph, graphing Integers as strings

Time:10-24

"Gender" is stored as an integer: 1 and 0 I am trying to graph it in a bar chart as "Male" and "female" on the X-axis compared with total number of bets by gambler.

I think there is an easier way of doing this rather than changing every input in the dataset into a string.

I am than trying to give male a blue color and female purple or yellow.

thank you everyone

code:

# scatter plot of age of Gamblers correlated with number of bets 
#alpha keeps from over plotting remove if unnecessary
p_1 <- ggplot(data = data, aes(x = Gender, y = BetsA ))  
    geom_point(alpha = 0.1)
p_1   ggtitle(label = "Gender Correlated with Total Number of Bets")   # for the main title
xlab(label = "Gender of Gambler")   # for the x axis label
ylab(label = "Total Number of Bets" ) # for the y axis label

current viz

CodePudding user response:

One option would be to first convert your Gender column to a factor. Afterwards you could use the labels argument of scale_x_discrete to assign your desired labels for 0 and 1. And for coloring you could basically do the same. Just map factor(Gender) on the color aes then set your desired colors via the values argument of scale_color_manual:

Using some fake random example data:

set.seed(123)

# Create example data
data <- data.frame(
  Gender = rep(c(0,1), 100),
  BetsA = runif(200, 0, 40000)
)

library(ggplot2)

ggplot(data = data, aes(x = factor(Gender), y = BetsA, color = factor(Gender) ))  
  geom_point(alpha = 0.1)  
  scale_x_discrete(labels = c("0" = "Male", "1" = "Female"))  
  scale_color_manual(values = c("0" = "blue", "1" = "purple"), labels = c("0" = "Male", "1" = "Female"))  
  ggtitle(label = "Gender Correlated with Total Number of Bets")  
  xlab(label = "Gender of Gambler")  
  ylab(label = "Total Number of Bets" )

  • Related