Bar charts visualize counts of unique data point values in the sample space of a categorical variable.
cities <- sample(c("NYC", "Boston", "LA", "Seattle"),
100,
replace = TRUE)
table(cities)
## cities
## Boston LA NYC Seattle
## 24 26 31 19
as.numeric(table(cities))
## [1] 24 26 31 19
names(table(cities))
## [1] "Boston" "LA" "NYC" "Seattle"
p1 <- plot_ly(x = names(table(cities)),
y = as.numeric(table(cities)),
name = "Cities",
type = "bar")
p1
p2 <- plot_ly(x = names(table(cities)),
y = as.numeric(table(cities)),
name = "Cities",
type = "bar") %>%
layout(title = "Number of offices in each city",
xaxis = list(title = "Cities",
zeroline = FALSE),
yaxis = list(title = "Number",
zeroline = FALSE))
p2
data.framedf <- data.frame(Cities = cities,
Group = sample(c("A", "B"),
100,
replace = TRUE))
head(df)
## Cities Group
## 1 NYC A
## 2 LA B
## 3 LA A
## 4 LA A
## 5 Seattle A
## 6 LA A
Greate two new data.frame object for groups A and B.
groupA <- df %>% filter(Group == "A")
groupB <- df %>% filter(Group == "B")
table(groupA$Cities)
##
## Boston LA NYC Seattle
## 11 14 14 9
names(table(groupA$Cities))
## [1] "Boston" "LA" "NYC" "Seattle"
as.numeric(table(groupA$Cities))
## [1] 11 14 14 9
gBarChart <- data.frame(Cities = names(table(groupA$Cities)),
GroupA = as.numeric(table(groupA$Cities)),
GroupB = as.numeric(table(groupB$Cities)))
head(gBarChart)
## Cities GroupA GroupB
## 1 Boston 11 13
## 2 LA 14 12
## 3 NYC 14 17
## 4 Seattle 9 10
p3 <- plot_ly(gBarChart,
x = ~Cities,
y = ~GroupA,
type = "bar",
name = "Group A") %>%
add_trace(y = ~GroupB,
name = "Group B") %>%
layout(yaxis = list(title = "Cities"),
barmode = "group")
p3
p4 <- plot_ly(gBarChart,
x = ~Cities,
y = ~GroupA,
type = "bar",
name = "group A") %>%
add_trace(y = ~GroupB,
name = "Group B") %>%
layout(yaxis = list(title = "Cities"),
barmode = "stack")
p4
p5 <- plot_ly(x = names(table(cities)),
y = as.numeric(table(cities)),
name = "Cities",
type = "bar",
marker = list(color = "rgba(255, 70, 0, 0.7)",
line = list(color = "rgba(0, 0, 0, 0.5)",
width = 1.5))) %>%
layout(title = "Number of offices per city",
xaxis = list(title = "Cities",
zeroline = FALSE),
yaxis = list(title = "Number",
zeroline = FALSE))
p5
The tickangle = argument in the xaxis ragument of the layout command can change the angle of text.
p6 <- plot_ly(x = names(table(cities)),
y = as.numeric(table(cities)),
name = "Cities",
type = "bar",
marker = list(color = "rgba(255, 70, 0, 0.7)",
line = list(color = "rgba(0, 0, 0, 0.5)",
width = 1.5))) %>%
layout(title = "Number of offices per city",
xaxis = list(title = "Cities",
zeroline = FALSE,
tickangle = -20),
yaxis = list(title = "Number",
zeroline = FALSE))
p6
Color can be specified of each bar.
p7 <- plot_ly(x = names(table(cities)),
y = as.numeric(table(cities)),
name = "Cities",
type = "bar",
marker = list(color = c("rgba(150, 150, 150, 0.7)",
"rgba(150, 150, 150, 0.7",
"rgba(255, 20, 0, 0.7)",
"rgba(150, 150, 150, 0.7"))) %>%
layout(title = "Number of offices per city",
xaxis = list(title = "Cities",
zeroline = FALSE),
yaxis = list(title = "Number",
zeroline = FALSE))
p7