#Homework 3 Rachel Konshok R markdown file
#5.1 Basic bar chart #Notes: first load plotly library , then create basic bar with x and y. From video x and y characteristics have been changed library(plotly)
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
fig = plot_ly(
x = c("horses", "donkeys", "cows"),
y = c(12,38,27),
name = "SF Petting Zoo",
type = "bar"
)
fig
#Grouped bar chart 5.2
#Again animals were changed as well as some values from SFzoo and LAzoo, then LAzoo was used to build the plot and SF zoo was added with trace. Which is inverse of what was shown in the lessons.
Animals = c("Horses", "donkeys", "cows")
SFzoo = c(12,14,28)
LAzoo= c(12,18,28)
data = data.frame(Animals, SFzoo, LAzoo)
fig2 = plot_ly(data, x =~Animals, y = ~LAzoo, type = 'bar', name = 'la Pet Zoo')
fig2 = fig2 %>% add_trace(y=~SFzoo, name = 'sf pet Zoo')
fig2 = fig2 %>% layout(yaxis=list(title='Number'), barmode = 'group')
fig2
#Stacked Bar Chart 5.3 #Same modifications were made as above, this time using barmode ‘stack’.
Animals = c("Horses", "donkeys", "cows")
SFzoo = c(12,14,28)
LAzoo= c(12,18,28)
data = data.frame(Animals, SFzoo, LAzoo)
fig2 = plot_ly(data, x =~Animals, y = ~LAzoo, type = 'bar', name = 'la Pet Zoo')
fig2 = fig2 %>% add_trace(y=~SFzoo, name = 'sf pet Zoo')
fig2 = fig2 %>% layout(yaxis=list(title='Count'), barmode = 'stack')
fig2
#Bar chart with Hover text 5.4 #Values of y were slightly changed, and colors of both the bar and line were changed. So was width of the line.
x = c('A', 'B', 'C')
y = c(20,32,23)
text = c('27% market share', '32% market share', '19% market share')
data2 = data.frame(x,y, text)
fig4 = plot_ly(data2, x =~x, y =~y, type = 'bar', text = text,
marker = list(color = 'rgb(300,202,225)',
line = list(color = 'rgb(99,48,107)',
width = 1.0)))
fig4 = fig4 %>% layout(title = "January 2013 Sales Report",
xaxis = list(title = ""),
yaxis = list(title = ""))
fig4
#5.5 Bar chart with direct labels
#Direct labels were added here by changing text to y instead of text values as in last plot for line 91. I changed the color of the markers to a dark yellow, and I added a x and y axis title. I also deleted the text values from above as they’re not needed nor utilized here.
x = c('Product A', 'Product B', 'Product C')
y = c(20,14,23)
data2 = data.frame(x,y)
fig4 = plot_ly(data2, x =~x, y =~y, type = 'bar', text = y, textposition = 'auto',
marker = list(color = 'rgb(230,180,90)',
line = list(color = 'rgb(8,48,107)',
width = 1.5)))
fig4 = fig4 %>% layout(title = "january 2013 Sales Report",
xaxis = list(title = "Product"),
yaxis = list(title = "% Market Share"))
fig4
#5.6 grouped bar chart with direct labels
#I deleted the text values here again as they weren’t needed. So the data.frame is now x y and y2. I changed the color for both the markers (bars), the width for each outside line of the markers was reduced to 1 from 1.5.
x = c('Product A', 'Product B', 'Product C')
y = c(20,14,23)
y2= c(16,12,27)
data2 = data.frame(x,y,y2)
fig4 = plot_ly(data2, x =~x, y =~y, type = 'bar', text = y, textposition = 'auto',
marker = list(color = 'rgb(400,202,225)',
line = list(color = 'rgb(8,48,107)',
width = 1)))
fig4 = fig4 %>% add_trace(x =~x, y = ~y2, type = 'bar',
text = y2, textposition = 'auto',
marker = list(color = 'rgb(100,202,225)',
line = list(color = 'rgb(8,48,107)',
width = 1)))
fig4 = fig4 %>% layout(title = "january 2013 Sales Report",
xaxis = list(title = ""),
yaxis = list(title = ""))
fig4
#5.7 Rotated bar chart labels #The names on the legend were changed slightly, I changed the color of markers, and the tickangle was adjust to be nearly verticle.
x = c('January', 'Feburary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November','December')
y1=c(20,14,25,16,18,22,19,15,12,16,14,17)
y2=c(19,14,22,14,16,19,15,14,10,12,12,16)
data=data.frame(x,y1,y2)
data$x = factor(data$x, levels = data[["x"]])
fig = plot_ly(data,x=~x, y = ~y1, type = 'bar', name = 'Product A', marker = list(color = 'rgb(49,200,189('))
fig = fig %>% add_trace(y=~y2, name = 'Product B', marker = list(color = 'rgb(150,204,204)'))
fig = fig %>% layout(xaxis = list(title = "", tickangle = -85),
yaxis = list(title = ""),
margin = list(b = 100),
barmode = 'group')
fig
#5.8 others done on other computer customizing bar color
#The values of y were changed for certain variables. The color was changed from black to red.
x = c('Feature A', 'Feature B', 'Feature C', 'Feature D', 'Feature E')
y = c(20,30,23,18,22)
data = data.frame(x,y)
fig = plot_ly(data, x = ~x , y = ~y, type = 'bar', color = I("red"))
fig = fig %>% layout(title = "Features",
xaxis = list(title = ""),
yaxis = list(title = ""))
fig
#5.9 Customizing individual bar colors #Values of y were changed, then the colors were adjusted for each marker. Feature D and E are the same color with an adjustment to transparancy values.
x = c('Feature A', 'Feature B', 'Feature C', 'Feature D', 'Feature E')
y = c(18,34,13,35,27)
data = data.frame(x,y)
fig = plot_ly(data, x = ~x , y = ~y, type = 'bar',
marker = list(color = c('rgba(104,204,204,1)', 'rgba(222,100,38,.8)',
'rgba(204,204,100,1)', 'rgba(204,204,204,1)',
'rgba(204,204,204,.5)')))
fig = fig %>% layout(title = "Least Used Features",
xaxis = list(title = ""),
yaxis = list(title = ""))
fig
#5.10 Customizing Individual Bar Widths #The widths were changed for each variable and the x value was changed as well to make sure none were overlapping/touching.
x= c(1,4,6,10,15)
y = c(10,8,6,4,2)
width = c(2, .8, 1, 3.5,.5)
data = data.frame(x,y,width)
fig = plot_ly(data)
fig = fig %>% add_bars(x = ~x, y = ~y, width = ~width)
fig = fig %>% layout(title = "Customizing individual bar widths",
xaxis = list(title = ""),
yaxis = list(title = ""))
fig
#5.11 Customizing individual bar base
#I changed the values for the ‘negative’ bars, I also changed colors to black and orange.
fig = plot_ly()
fig = fig %>% add_bars(x = c("2016", "2017", "2018"),
y = c(600,800,700),
base = c(-600,-800,-700),
marker = list(
color = 'black'),
name = 'expenses')
fig =fig %>% add_bars(x = c("2016", "2017", "2018"),
y = c(300,400,700),
base = c(0),
marker = list(
color = 'orange'),
name = 'revenue')
fig
#5.12 Waterfall Bar Chart
#The colors of each bar were slightly changed, I also changed the annotation font and font size.
x= c('Product<br>Revenue', 'Services<br>Revenue', 'Total<br>Revenue', 'Fixed<br>Costs', 'Variable<br>Costs', 'Total<br>Costs', 'Total')
y = c(400,600,660,590,400,400,340)
base = c(0,430, 0 , 570, 370, 370, 0)
revenue = c(430,260,690,0,0,0,0)
costs =c(0,0,0,120,200,320,0)
profit = c(0,0,0,0,0,0,370)
text = c('$430K', '$260K', '$690K', '$-120K', '$-200k', '$-320K', '$370K')
data = data.frame(x, base, revenue, costs, profit, text)
data$x = factor(data$x, level = data[["x"]])
fig = plot_ly(data, x = ~x, y = ~base, type = 'bar', marker = list(color = 'rgba(1,1,1,0.0)'))
fig = fig %>% add_trace(y=~revenue, marker = list(color = 'rgba(60,128,191,.7)',
line = list(color = 'rgba(55,128,191,.7)',
width = 2)))
fig = fig %>% add_trace(y=~costs, marker = list(color = 'rgba(200,64,82,.7)',
line = list(color = 'rgba(219,64,82,1.0)',
width = 2)))
fig = fig %>% add_trace(y=~profit, marker = list(color = 'rgba(100,171,96,.7)',
line = list(color = 'rgba(50,171,96,1.0)',
width = 2)))
fig = fig %>% layout(title = 'Annual Profit - 2015',
xaxis = list(title = ""),
yaxis = list(title = ""),
barmode = 'stack',
paper_bgcolor = 'rgba(245,246,249,1)',
plot_bgcolor = 'rgba(245,246,249,1)',
showlegend = FALSE)
fig = fig %>% add_annotations(text = text,
x =x,
y = y,
xref = "x",
yref = "y",
font = list(family = 'Times New Roman',
size = 12,
color = 'rgba(245,246,249,1)'),
showarrow = FALSE)
fig
##Chapter 6. Horizontal Bar Charts #The values for both x and y were changed for this chart.
library(plotly)
fig = plot_ly(x=c(20,12,27), y = c('donkeys', 'horses', 'cows'), type = 'bar', orientation = 'h')
fig
#6.2 Colored Horizontal Stacked Bar Chart
#I changed the color of each bar and their respective borders, as well as doubled the border size from it’s original width.
data = data.frame(Animals, SFzoo, LAzoo)
fig = plot_ly(data, x =~SFzoo, y =~Animals, type = 'bar', orientation = 'h', name = 'SF Zoo',
marker =list(color = 'rgba(150,78,139,.6)',
line = list(color = 'rgba(150,78,139, 1.0)',
width = 6)))
fig = fig %>% add_trace(x=~LAzoo, name = 'La Zoo',
marker = list(color = 'rgba(350,71,80,.6)',
line = list(color = 'rgba(350,71,80,1.0)',
width = 6)))
fig = fig %>% layout(barmode = 'stack',
xaxis = list(title = ""),
yaxis = list(title = ""))
fig
#6.3 Color Palette for Bar Chart
#I changed the font and size for annotations, as well as labels, I
also changed the
(break points) in y values.
y <- c('The course was effectively<br>organized',
'The course developed my<br>abilities and skills<br>for the subject',
'The course developed my<br>ability to think<br>critically about<br>the subject',
'I would recommend this<br>course to a friend')
x1 <- c(21, 24, 27, 29)
x2 <-c(30, 31, 26, 24)
x3 <- c(21, 19, 23, 15)
x4 <- c(16, 15, 11, 18)
x5 <- c(12, 11, 13, 14)
data <- data.frame(y, x1, x2, x3, x4, x5)
top_labels <- c('Strongly<br>agree', 'Agree', 'Neutral', 'Disagree', 'Strongly<br>disagree')
#orientation = 'h' gives a horizontal orientation. while marker desginates the marker in this case the bars (bar chart) attributes such as color, border, border width, etc.
fig <- plot_ly(data, x = ~x1, y = ~y, type = 'bar', orientation = 'h',
marker = list(color = 'rgba(38, 24, 74, 0.8)',
line = list(color = 'rgb(248, 248, 249)', width = 1)))
#add_trace addes another trace to a value from the data.frame not yet outlined on the graph each add_trace here addes a bar for x2, x3, x4, and x5 respectively
fig <- fig %>% add_trace(x = ~x2, marker = list(color = 'rgba(71, 58, 131, 0.8)'))
fig <- fig %>% add_trace(x = ~x3, marker = list(color = 'rgba(122, 120, 168, 0.8)'))
fig <- fig %>% add_trace(x = ~x4, marker = list(color = 'rgba(164, 163, 204, 0.85)'))
fig <- fig %>% add_trace(x = ~x5, marker = list(color = 'rgba(190, 192, 213, 1)'))
fig <- fig %>% layout(xaxis = list(title = "",
showgrid = FALSE,
showline = FALSE,
showticklabels = FALSE,
zeroline = FALSE,
domain = c(0.15, 1)),
yaxis = list(title = "",
showgrid = FALSE,
showline = FALSE,
showticklabels = FALSE,
zeroline = FALSE),
barmode = 'stack',
paper_bgcolor = 'rgb(248, 248, 255)', plot_bgcolor = 'rgb(248, 248, 255)',
margin = list(l = 120, r = 10, t = 140, b = 80),
showlegend = FALSE)
# labeling the y-axis
fig <- fig %>% add_annotations(xref = 'paper', yref = 'y', x = 0.14, y = y,
xanchor = 'right',
text = y,
font = list(family = 'Arial', size = 16,
color = 'rgb(67, 67, 67)'),
showarrow = FALSE, align = 'right')
# labeling the percentages of each bar (x_axis)
#here is where i changed the font size and font making the chart look a bit weird.
fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
x = x1 / 2, y = y,
text = paste(data[,"x1"], '%'),
font = list(family = 'Luminari', size = 12,
color = 'rgb(248, 248, 255)'),
showarrow = FALSE)
fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
x = x1 + x2 / 2, y = y,
text = paste(data[,"x2"], '%'),
font = list(family = 'mono', size = 10,
color = 'rgb(248, 248, 255)'),
showarrow = FALSE)
fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
x = x1 + x2 + x3 / 2, y = y,
text = paste(data[,"x3"], '%'),
font = list(family = 'Optima', size = 12,
color = 'rgb(248, 248, 255)'),
showarrow = FALSE)
fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
x = x1 + x2 + x3 + x4 / 2, y = y,
text = paste(data[,"x4"], '%'),
font = list(family = 'sans', size = 18,
color = 'rgb(248, 248, 255)'),
showarrow = FALSE)
fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
x = x1 + x2 + x3 + x4 + x5 / 2, y = y,
text = paste(data[,"x5"], '%'),
font = list(family = 'Times New Roman', size = 12,
color = 'rgb(248, 248, 255)'),
showarrow = FALSE)
# labeling the first Likert scale (on the top)
fig <- fig %>% add_annotations(xref = 'x', yref = 'paper',
x = c(21 / 2, 21 + 30 / 2, 21 + 30 + 21 / 2, 21 + 30 + 21 + 16 / 2,
21 + 30 + 21 + 16 + 12 / 2),
y = 1.15,
text = top_labels,
font = list(family = 'comic', size = 24,
color = 'rgb(67, 67, 67)'),
showarrow = FALSE)
fig
``
#6.4 Bar Chart with line plot
#I changed quite a bit of the color, the line width on the bars as well as the bar color. The background color of the entire plot, changed the font for the doi on the bottom. I also changed the line graph to just lines instead of lines and markers.
y <- c('Japan', 'United Kingdom', 'Canada', 'Netherlands', 'United States', 'Belgium', 'Sweden', 'Switzerland')
x_saving <- c(1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996,
7.4812000000000003, 7.5133000000000001, 15.2148, 17.520499999999998)
x_net_worth <- c(93453.919999999998, 81666.570000000007, 69889.619999999995, 78381.529999999999,
141395.29999999999, 92969.020000000004, 66090.179999999993, 122379.3)
data = data.frame(y, x_saving, x_net_worth)
#Changed color below and line color as well as line width
fig1 = plot_ly(x = ~x_saving, y = ~reorder(y, x_saving), name = 'Household savings, percnetage of household disposable income',
type = 'bar', orientation = 'h',
marker = list(color = 'rgba(150,171,96,.6)',
line = list(color = 'rgba(150,171,96,1.0)', width = 2)))
fig1 =fig1 %>% layout(yaxis=list(showgrid = FALSE, showline = FALSE, showticklabels = TRUE, domain = c(0, .85)),
xaxis = list(zeroline = FALSE, showline = FALSE, showticklabels = TRUE, showgrid = TRUE))
fig1 =fig1 %>% add_annotations(xref = 'x1', yref = 'y',
x = x_saving *2.1 + 3, y=y,
text = paste(round(x_saving, 2), '%'),
font = list(family = 'Arial', size = 12, color = 'rgb(50,171,96)'),
showarrow = FALSE)
#changed below graph to just lines instead of lines and markers
fig2 = plot_ly(x = ~x_net_worth, y = ~reorder(y, x_saving), name = 'Household new worth, Million USD/capita',
type = 'scatter', mode = 'lines',
line = list(color = 'rgb(128,0,128)'))
fig2 = fig2 %>% layout(yaxis=list(showgrid = FALSE, showline = TRUE, showticklabels = FALSE,
linecolor = 'rgba(102,102,102,.8)', linewidth = 2,
domain = c(0, .85)),
xaxis = list(zeroline = FALSE, showline = FALSE, showticklabels = TRUE, showgrid = TRUE, side = 'top', dtick = 25000))
fig2 = fig2 %>% add_annotations(xref = 'x2', yref = 'y',
x = x_net_worth, y = y,
text = paste(x_net_worth, 'M'),
font = list(family = 'Arial', size = 12, color = 'rgb(128,0,128)'),
showarrow = FALSE)
fig = subplot(fig1,fig2)
fig = fig %>% layout(title = 'Household savings & net worth for eight OECD countries',
legend = list(x = .029, y = 1.038,
font = list(size = 10)),
margin = list(l = 100, r =40, t = 70, b= 70),
paper_bgcolor = 'rgb(150,248,255)',
plot_bgcolor = 'rgb(150,248, 255)')
#changed color and shifted slightly more right
fig = fig %>% add_annotations(xref = 'paper', yref = 'paper',
x = -.14, y = -.15,
text = paste('OECD (2015), Household savings (indicator), Household net worth (indicator). doi:10.1787/cfc6f499-en (Accessed on 05 June 2015)'),
font = list(family = 'Times New Romans', size = 12, color = 'rgb(200,150,150)'),
showarrow = FALSE)
#changed font and font size above
fig
##Chapter 7 Pie Charts
#7.1 Basic pie chart
#Honestly this one was so basic I wasn’t sure what to change.I slightly changed the name?
USPersonalExpenditure <- data.frame("Categorie"=rownames(USPersonalExpenditure), USPersonalExpenditure)
data <- USPersonalExpenditure[,c('Categorie', 'X1960')]
fig = plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie')
fig = fig %>% layout(title = 'United States Personal Expenditures in 1960',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
fig
#7.2 Styled Pie Chart
#I changed the colors a bit, as well as making the line width bigger.
USPersonalExpenditure <- data.frame("Categorie"=rownames(USPersonalExpenditure), USPersonalExpenditure)
data <- USPersonalExpenditure[,c('Categorie', 'X1960')]
color = c('rgb(250,94,96)', 'rgb(128,133,133, .5)', 'rgb(144,103,167)', 'rgb(1711,104,87)',
'rgb(114,147,203)')
fig = plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie',
textposition = 'inside',
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
hovertext = 'text',
text = ~paste('$',X1960, 'billions'),
marker = list(colors = color,
line = list(color = '#FFFFFF', width = 3)),
showlegend = FALSE)
fig = fig %>% layout(title = 'United States Personal Expeditures by Categories in 1960',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis =list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
fig
#7.3 Subplots # I added the legend and changed the domain position of the top two pie charts moving them to 30% and 70% of the x domain repectively. And reduced their y domain from 60 to 50%.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
fig <- plot_ly()
fig <- fig %>% add_pie(data = count(diamonds, cut), labels = ~cut, values = ~n,
name = "Cut", domain = list(x = c(0, 0.3), y = c(0.5, 1)))
fig <- fig %>% add_pie(data = count(diamonds, color), labels = ~color, values = ~n,
name = "Color", domain = list(x = c(0.7, 1), y = c(0.5, 1)))
fig <- fig %>% add_pie(data = count(diamonds, clarity), labels = ~clarity, values = ~n,
name = "Clarity", domain = list(x = c(0.25, 0.75), y = c(0, 0.6)))
fig <- fig %>% layout(title = "Pie Charts with Subplots", showlegend = T,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
fig
#7.4 Subplots using grid
#I switched the pie chart spots of color and clarity with eachother
fig <- plot_ly()
fig <- fig %>% add_pie(data = count(diamonds, cut), labels = ~cut, values = ~n,
name = "Cut", domain = list(row = 0, column = 0))
fig <- fig %>% add_pie(data = count(diamonds, color), labels = ~color, values = ~n,
name = "Color", domain = list(row = 1, column = 0))
fig <- fig %>% add_pie(data = count(diamonds, clarity), labels = ~clarity, values = ~n,
name = "Clarity", domain = list(row = 0, column = 1))
fig <- fig %>% add_pie(data = count(diamonds, cut), labels = ~cut, values = ~n,
name = "Clarity", domain = list(row = 1, column = 1))
fig <- fig %>% layout(title = "Pie Charts with Subplots", showlegend = F,
grid=list(rows=2, columns=2),
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
fig
#7.5 Controlling text orientation inside sunburst sectors
#I changed the intext info to just percent instead of label and percent, and the inside text orientation to horizontal.
labels = c('Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen')
values = c(4500, 2500, 1053, 500)
fig = plot_ly(type = 'pie', labels = labels, values = values,
textinfo = 'percent',
insidetextorientation = 'horizontal')
fig
#7.6 donut chart
#I added the legend and reduced the hole from .6 to .4.
mtcars$manuf <- sapply(strsplit(rownames(mtcars), " "), "[[", 1)
df <- mtcars
df <- df %>% group_by(manuf)
df <- df %>% summarize(count = n())
fig <- df %>% plot_ly(labels = ~manuf, values = ~count)
fig <- fig %>% add_pie(hole = .4)
fig <- fig %>% layout(title = "Donut charts using Plotly", showlegend = T,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
fig