I have a bar chart like below, which is drawn by the plotly package.
You can see the data and code below:
#CODE
#Data
test_data_set<-structure(list(Name = c("Cars_sale_1", "Cars_sale_2", "Cars_sale_3",
"Cars_sale_4", "Cars_sale_5", "Cars_sale_6", "Cars_sale_7", "Cars_sale_8",
"Cars_sale_9", "Cars_sale_10", "Cars_sale_11", "Cars_sale_12",
"Cars_sale_13"), First = c(156300.824706096, 10006.2467099491,
3212.0722933848, 3319.03842779435, 9658.39620986138, 8434.32181084401,
1367.81891559923, 717.880329882435, 260.817687313564, 196.525706264257,
1042.98999824531, 7036.46253728724, 14974.7002155131), Second = c(227324.372696964,
16086.4713107563, 6318.58220740481, 21832.8829619231, 15740.5860677312,
10538.8313739252, 4399.92981224776, 2872.64432356554, 1391.68275135989,
0, 1979.57536409896, 12618.0733462011, 20694.7337436906), Third = c(277421.301982804,
18264.5376381821, 10922.6180031584, 30805.9659589402, 23327.3205825583,
14162.2038954203, 9179.99649061239, 5272.22319705212, 3019.19635023688,
0, 3587.71714335848, 17227.7241621337, 21867.2276106995), Fourth = c(307141.042288121,
27274.1182663625, 15141.1826636252, 51266.257238112, 25035.1289699947,
18876.8555886998, 13549.8859449026, 12045.9027899632, 4577.92595192139,
0, 9101.66695911564, 19369.2928583962, 30971.5263285415), Fifth = c(345904.895595719,
35406.3519915775, 21022.9163011055, 70233.5146516933, 28311.4932444288,
22832.3565537814, 21108.8261098438, 14801.7546938059, 4776.69766625724,
56.1502017897877, 11680.6457273206, 24203.544481488, 25989.4022630561
), Sixth = c(375676.013335673, 38199.2630286015, 34954.3428671697,
96511.528338305, 33332.4442884717, 27694.4025267591, 27706.1940691349,
26899.0349184067, 8709.73855062292, 224.600807159151, 16098.5436041411,
31910.4404281453, 32467.4847713049), Seventh = c(433176.346727496,
47455.623793648, 51832.251272153, 121340.024565713, 41695.1745920337,
31331.5318476926, 44969.8543604141, 24795.9291103702, 10157.0100017547,
828.215476399368, 27548.4120021056, 41680.0140375504, 35955.6910763933
), Eight = c(501520.687839972, 55052.4653447973, 74202.4916652044,
162651.693279523, 45550.4474469205, 40385.1903842779, 54554.132303913,
43609.6157220565, 16360.2035444815, 4171.95999298123, 45789.3665555361,
53713.5637831198, 29226.7897579876), Ninth = c(567436.251974031,
65858.0101772241, 104945.288647131, 238514.82716266, 60495.6659062993,
52381.4002456571, 100849.973679593, 61956.6941568696, 27927.4258641867,
4159.60694858747, 77211.5809791192, 69056.0449201614, 29472.1253015506
), Tenth = c(755730.057904896, 89047.2012633796, 208602.210914195,
544052.500438673, 195334.760484295, 129515.213195297, 220957.50131602,
119074.083172486, 115559.080540446, 36932.7952272328, 156449.622740832,
120385.751886296, 33197.0639513509)), row.names = c(NA, -13L), class = c("tbl_df",
"tbl", "data.frame"))
# Draw bar chart
library(plotly)
t_df <- data.frame(t(test_data_set[,-1]))
colnames(t_df) <- test_data_set$Name
t_df$Number <- factor(row.names(t_df),levels=row.names(t_df),ordered=TRUE)
p <- plot_ly(t_df, x = ~Number, y = ~Cars_sale_1, type = 'bar', name = 'Cars_sale_1') %>%
add_trace(y = ~Cars_sale_2, name = 'Cars_sale_2') %>%
add_trace(y = ~Cars_sale_3, name = 'Cars_sale_3') %>%
add_trace(y = ~Cars_sale_4, name = 'Cars_sale_4') %>%
add_trace(y = ~Cars_sale_5, name = 'Cars_sale_5') %>%
add_trace(y = ~Cars_sale_6, name = 'Cars_sale_6') %>%
add_trace(y = ~Cars_sale_7, name = 'Cars_sale_7') %>%
add_trace(y = ~Cars_sale_8, name = 'Cars_sale_8') %>%
add_trace(y = ~Cars_sale_9, name = 'Cars_sale_9') %>%
add_trace(y = ~Cars_sale_10, name = 'Cars_sale_10') %>%
add_trace(y = ~Cars_sale_11, name = 'Cars_sale_11') %>%
add_trace(y = ~Cars_sale_12, name = 'Cars_sale_12') %>%
add_trace(y = ~Cars_sale_13, name = 'Cars_sale_13') %>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')
p
The chart shows 13 different categories but my problem are the colors.
Car_sale_1 and Car_sale_11 have the same color, as do Car_sale_2 and Car_sale_12 etc.
My intention is to make a graph with unique colors per category. Can anybody help me fix this problem ?
I have reedited the code to make it much easier for you to see a possible way to manually select the colors. Also, do not add as many traces manually since you would get crazy if you had 100. Do it the following way:
library(plotly)
library(reshape2)
t_df <- data.frame(t(test_data_set[,-1]))
colnames(t_df) <- test_data_set$Name
t_df$Number <- factor(row.names(t_df),levels=row.names(t_df),ordered=TRUE)
t_df<-melt(t_df, id.vars = c("Number"))
t_df$color <- factor(t_df$variable, labels = c("blue", "red", "green", "yellow", "brown",
"black","orange","purple", "grey","white",
"chocolate", "coral", "cornflowerblue"))
p <- plot_ly(t_df, x = ~Number, y = ~value,
type = 'bar',
marker = list(color = ~color), name = ~variable) %>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')
p
Related
I am trying to add a second category to x axis with Plotly under R like this:
Here is my code:
library(plotly)
data <- data.frame(day= c(1:4),
visit = c("visit1","visit1", "visit2", "visit2"),
val = c(1:4),
flag = c("","","", 1))
fig <- plot_ly(data= data, x = ~day) %>%
add_trace(y = ~val,
type = 'scatter',
mode = 'lines+markers',
line = list(width = 2,
dash = 'solid')) %>%
add_trace(data= data %>% filter(flag == 1), y = 0,
type = 'scatter',
hoverinfo = "text",
mode = 'markers',
name = "flag",
text = paste("<br>N°",data$ID[data$flag == 1],
"<br>Day",data$day[data$flag == 1]),
marker = list(
color = 'red',
symbol = "x",
size = 12
),
showlegend = T
)
fig
I have tried this, which seems good but the markers disappear from the graph, maybe due to the filter on data.
library(plotly)
data <- data.frame(day= c(1:4),
visit = c("visit1","visit1", "visit2", "visit2"),
val = c(1:4),
flag = c("","","", 1))
fig <- plot_ly(data= data, x = ~list(visit,day)) %>%
add_trace(y = ~val,
type = 'scatter',
mode = 'lines+markers',
line = list(width = 2,
dash = 'solid')) %>%
add_trace(data= data %>% filter(flag == 1), y = 0,
type = 'scatter',
hoverinfo = "text",
mode = 'markers',
name = "flag",
text = paste("<br>N°",data$ID[data$flag == 1],
"<br>Day",data$day[data$flag == 1]),
marker = list(
color = 'red',
symbol = "x",
size = 12
),
showlegend = T
)
fig
You didn't provide a reproducible question, so I've made data. (Well, data I shamelessly stole from here). This creates a bar graph with two sets of x-axis labels. One for each set of bars. One for each group of bars. The content of the x-axis is the same in both traces.
library(plotly)
fig <- plot_ly() %>%
add_bars(x = list(rep(c("first", "second"), each = 2),
rep(LETTERS[1:2], 2)),
y = c(2, 5, 2, 6),
name = "Adults") %>%
add_bars(x = list(rep(c("first", "second"), each = 2),
rep(LETTERS[1:2], 2)),
y = c(1, 4, 3, 6),
name = "Children")
fig
Update
You added data and code trying to apply this to your data. I added an update and apparently missed what the problem was. Sorry about that.
Now that I'm paying attention (let's hope, right?), here is an actual fix to the actual problem.
For this change, I modified your data. Instead of the flag annotated with a one, I changed it to zero. Then I used flag as a variable.
data <- data.frame(day = c(1:4),
visit = c("visit1","visit1", "visit2", "visit2"),
val = c(1:4),
flag = c("","","", 0))
fig <- plot_ly(data= data, x = ~list(visit,day)) %>%
add_trace(y = ~val,
type = 'scatter', mode = 'lines+markers',
line = list(width = 2,
dash = 'solid')) %>%
add_trace(y = ~flag,
type = 'scatter', hoverinfo = "text",
mode = 'markers', name = "flag",
text = paste("<br>N°",data$ID[data$flag == 1],
"<br>Day",data$day[data$flag == 1]),
marker = list(
color = 'red', symbol = "x", size = 12),
showlegend = T)
fig
You're going to get a warning about discrete & non-discrete data, which isn't really accurate, but it shows up, nonetheless.
I want to show the date for every 1 month, but it shows every 2 months instead. I also wonder how to tilt the date (x axis) 60 degree in plotly. Is these two things possible in plotly? Thanks in advance.
My code:
l <- list(
font = list(
family = "sans-serif",
size = 12,
color = "#364E6F"),
bgcolor = "#E4EBF5",
bordercolor = "#566F92",
borderwidth = 3)
fig <- plot_ly(sumir_data, x = sumir_data$`Month-year`, y = sumir_data$`Followers-LI`, type = 'bar', name = 'LinkedIn', marker = list(color = '#28EDC4'))
fig <- fig %>% add_trace(y = sumir_data$`EOM Followers Twitter-TW`, name = 'Twitter', marker = list(color = '#00BBFF'))
fig <- fig %>% layout(yaxis = list(title = 'Followers Count',
color = '#364E6F'))
fig <- fig %>% layout(xaxis = list(title = 'Date',
color = '#364E6F'))
fig <- fig %>% layout(legend = l)
fig <- fig %>% layout(paper_bgcolor = '#E4EBF5',
plot_bgcolor = '#E4EBF5')
fig
The solution would be to add dticks = "M1" to the xaxis layout()
My code:
l <- list(
font = list(
family = "sans-serif",
size = 12,
color = "#364E6F"),
bgcolor = "#FFFFFF",
bordercolor = "#566F92",
borderwidth = 3)
fig <- plot_ly(sumir_data, x = sumir_data$`Month-year`, y = sumir_data$`Followers-LI`, type = 'bar', name = 'LinkedIn', marker = list(color = '#28EDC4'))
fig <- fig %>% add_trace(y = sumir_data$`EOM Followers Twitter-TW`, name = 'Twitter', marker = list(color = '#00BBFF'))
fig <- fig %>% layout(yaxis = list(title = 'Number of Followers',
color = '#364E6F'))
fig <- fig %>% layout(xaxis = list(title = 'Date',
color = '#364E6F', dtick = "M1"))
fig <- fig %>% layout(legend = l)
fig <- fig %>% layout(paper_bgcolor = '#FFFFFF',
plot_bgcolor = '#FFFFFF')
fig
I have one data frame (matrix 13x11).You can see pic below
There are also data :
# Data
test_data_set<-structure(list(Name = c("Cars_sale_1", "Cars_sale_2", "Cars_sale_3",
"Cars_sale_4", "Cars_sale_5", "Cars_sale_6", "Cars_sale_7", "Cars_sale_8",
"Cars_sale_9", "Cars_sale_10", "Cars_sale_11", "Cars_sale_12",
"Cars_sale_13"), First = c(156300.824706096, 10006.2467099491,
3212.0722933848, 3319.03842779435, 9658.39620986138, 8434.32181084401,
1367.81891559923, 717.880329882435, 260.817687313564, 196.525706264257,
1042.98999824531, 7036.46253728724, 14974.7002155131), Second = c(227324.372696964,
16086.4713107563, 6318.58220740481, 21832.8829619231, 15740.5860677312,
10538.8313739252, 4399.92981224776, 2872.64432356554, 1391.68275135989,
0, 1979.57536409896, 12618.0733462011, 20694.7337436906), Third = c(277421.301982804,
18264.5376381821, 10922.6180031584, 30805.9659589402, 23327.3205825583,
14162.2038954203, 9179.99649061239, 5272.22319705212, 3019.19635023688,
0, 3587.71714335848, 17227.7241621337, 21867.2276106995), Fourth = c(307141.042288121,
27274.1182663625, 15141.1826636252, 51266.257238112, 25035.1289699947,
18876.8555886998, 13549.8859449026, 12045.9027899632, 4577.92595192139,
0, 9101.66695911564, 19369.2928583962, 30971.5263285415), Fifth = c(345904.895595719,
35406.3519915775, 21022.9163011055, 70233.5146516933, 28311.4932444288,
22832.3565537814, 21108.8261098438, 14801.7546938059, 4776.69766625724,
56.1502017897877, 11680.6457273206, 24203.544481488, 25989.4022630561
), Sixth = c(375676.013335673, 38199.2630286015, 34954.3428671697,
96511.528338305, 33332.4442884717, 27694.4025267591, 27706.1940691349,
26899.0349184067, 8709.73855062292, 224.600807159151, 16098.5436041411,
31910.4404281453, 32467.4847713049), Seventh = c(433176.346727496,
47455.623793648, 51832.251272153, 121340.024565713, 41695.1745920337,
31331.5318476926, 44969.8543604141, 24795.9291103702, 10157.0100017547,
828.215476399368, 27548.4120021056, 41680.0140375504, 35955.6910763933
), Eight = c(501520.687839972, 55052.4653447973, 74202.4916652044,
162651.693279523, 45550.4474469205, 40385.1903842779, 54554.132303913,
43609.6157220565, 16360.2035444815, 4171.95999298123, 45789.3665555361,
53713.5637831198, 29226.7897579876), Ninth = c(567436.251974031,
65858.0101772241, 104945.288647131, 238514.82716266, 60495.6659062993,
52381.4002456571, 100849.973679593, 61956.6941568696, 27927.4258641867,
4159.60694858747, 77211.5809791192, 69056.0449201614, 29472.1253015506
), Tenth = c(755730.057904896, 89047.2012633796, 208602.210914195,
544052.500438673, 195334.760484295, 129515.213195297, 220957.50131602,
119074.083172486, 115559.080540446, 36932.7952272328, 156449.622740832,
120385.751886296, 33197.0639513509)), row.names = c(NA, -13L), class = c("tbl_df",
"tbl", "data.frame"))
My intention is to make two graph bar plots in R like pics below with package Plotly, which are drawn in Excel.
How do you draw a stacked bar plot with the Plotly package in R?
The problem is that your rows and columns have to be exchanged. Once it is done, you have to factor the order of First, Second etc in order for them to be printed in the right order
library(plotly)
t_df <- data.frame(t(test_data_set[,-1]))
colnames(t_df) <- test_data_set$Name
t_df$Number <- factor(row.names(t_df),levels=row.names(t_df),ordered=TRUE)
p <- plot_ly(t_df, x = ~Number, y = ~Cars_sale_1, type = 'bar', name = 'Cars_sale_1') %>%
add_trace(y = ~Cars_sale_2, name = 'Cars_sale_2') %>%
add_trace(y = ~Cars_sale_3, name = 'Cars_sale_3') %>%
add_trace(y = ~Cars_sale_4, name = 'Cars_sale_4') %>%
add_trace(y = ~Cars_sale_5, name = 'Cars_sale_5') %>%
add_trace(y = ~Cars_sale_6, name = 'Cars_sale_6') %>%
add_trace(y = ~Cars_sale_7, name = 'Cars_sale_7') %>%
add_trace(y = ~Cars_sale_8, name = 'Cars_sale_8') %>%
add_trace(y = ~Cars_sale_9, name = 'Cars_sale_9') %>%
add_trace(y = ~Cars_sale_10, name = 'Cars_sale_10') %>%
add_trace(y = ~Cars_sale_11, name = 'Cars_sale_11') %>%
add_trace(y = ~Cars_sale_12, name = 'Cars_sale_12') %>%
add_trace(y = ~Cars_sale_13, name = 'Cars_sale_13') %>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')
p
For the second graph I created a new dataframe where I make sure that every row has sum 1
t_df_pct <- t_df
t_df_pct[,1:(ncol(t_df)-1)] <- t_df_pct[,1:(ncol(t_df)-1)]/rowSums(t_df_pct[,1:(ncol(t_df)-1)])
p2 <- plot_ly(t_df_pct, x = ~Number, y = ~Cars_sale_1, type = 'bar', name = 'Cars_sale_1') %>%
add_trace(y = ~Cars_sale_2, name = 'Cars_sale_2') %>%
add_trace(y = ~Cars_sale_3, name = 'Cars_sale_2') %>%
add_trace(y = ~Cars_sale_4, name = 'Cars_sale_4') %>%
add_trace(y = ~Cars_sale_5, name = 'Cars_sale_5') %>%
add_trace(y = ~Cars_sale_6, name = 'Cars_sale_6') %>%
add_trace(y = ~Cars_sale_7, name = 'Cars_sale_7') %>%
add_trace(y = ~Cars_sale_8, name = 'Cars_sale_8') %>%
add_trace(y = ~Cars_sale_9, name = 'Cars_sale_9') %>%
add_trace(y = ~Cars_sale_10, name = 'Cars_sale_10') %>%
add_trace(y = ~Cars_sale_11, name = 'Cars_sale_11') %>%
add_trace(y = ~Cars_sale_12, name = 'Cars_sale_12') %>%
add_trace(y = ~Cars_sale_13, name = 'Cars_sale_13') %>%
layout(yaxis = list(title = 'Percentage'), barmode = 'stack')
p2
I've got three columns of data I would like to plot as a cumulative stacked area chart over a 10 day sampling period.
ID variable value
dallas sample.01 0.0012
austin sample.01 0.23
seattle sample.01 0.01
I'd like it to look something like this:
But it's coming out like this:
What am I doing wrong with my code?
melted_dat %>%
group_by(value,ID) %>%
plot_ly(
x = ~variable,
y = ~value,
color = ~ID,
type='scatter',
mode = 'none',
fill = 'tonexty',
stackgroup = 'one',
fillcolor = ~ID) %>%
layout(showlegend = FALSE)
I think you need to add the groups trace by trace. As in the following example (from here):
library(plotly)
data <- t(USPersonalExpenditure)
data <- data.frame("year"=rownames(data), data)
p <- plot_ly(data, x = ~year, y = ~Food.and.Tobacco, name = 'Food and Tobacco', type = 'scatter', mode = 'none', stackgroup = 'one', groupnorm = 'percent', fillcolor = '#F5FF8D') %>%
add_trace(y = ~Household.Operation, name = 'Household Operation', fillcolor = '#50CB86') %>%
add_trace(y = ~Medical.and.Health, name = 'Medical and Health', fillcolor = '#4C74C9') %>%
add_trace(y = ~Personal.Care, name = 'Personal Care', fillcolor = '#700961') %>%
add_trace(y = ~Private.Education, name = 'Private Education', fillcolor = '#312F44') %>%
layout(title = 'United States Personal Expenditures by Categories',
xaxis = list(title = "",
showgrid = FALSE),
yaxis = list(title = "Proportion from the Total Expenditures",
showgrid = FALSE,
ticksuffix = '%'))
# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="area-stackedcum")
chart_link
If you are following the cumulative example from plotly, don't do the group_by .... cumsum, I also get rid of fillcolor
I combined 2 charts and I am trying to add the second y-axis, but every time I add the yaxis = "y2" to my code, I lose have the my bar graphs.
> MediaDate Spend Search_Visits Other_Visits MediaDate2
> 2016-04-01 $39654.36 19970 2899 Apr 2016
> 2016-05-01 $34446.28 14460 2658 May 2016
> 2016-06-01 $27402.36 12419 2608 Jun 2016
my original code is:
p <- plot_ly(x= w$MediaDate2,y=w$Search_Visits,name = "Paid Search",
type = "bar")
p2 <- add_trace(p, x=w$MediaDate2, y=w$Other_Visits,name = "Other Traffic",
type = "bar")
spend_visits <- layout(p2, barmode = "stack")
spendvisits2 <- spend_visits %>% add_trace(data=w, x=MediaDate2, y=round(Spend,0), fill="tonexty", mode="lines",
text=w$MediaDate2, hoverinfo='name+y+text', name="Spend")
When I add the yaxis= "y2", only the area chart remains:
`spendvisits2 <- spend_visits %>% add_trace(data=w, x=MediaDate2, y=round(Spend,0), yxis="y2" fill="tonexty", mode="lines",
text=w$MediaDate2, hoverinfo='name+y+text', name="Spend")`
Any suggestions would be immensely helpful. Thank you
See this quick Plotly tutorial for multiple axes. You need to specify the attributes for the second y axis in layout().
df <- data.frame(MediaDate = as.Date(c("2016-04-01","2016-05-01","2016-06-01"), format = "%Y-%m-%d"),
Spend = c(39654, 34446, 27402),
Visits = c(19970, 14450, 12419))
plot_ly(df, x = ~MediaDate, y = ~Spend, type = "bar", name = "Spend") %>%
add_trace(x = ~MediaDate, y = ~Visits, mode = "lines", yaxis = "y2", name = "Visits") %>%
layout(yaxis2 = list(overlaying = "y", side = "right"))
Try this
plot_ly(df) %>%
add_trace(x = ~MediaDate, y = ~Spend,type = "bar", name = "Spend") %>%
add_trace(x = ~MediaDate, y = ~Visits, mode = "lines", yaxis = "y2", name = "Visits") %>%
layout(yaxis2 = list(overlaying = "y", side = "right"))
Adding type='scatter' to what Vance Lopez commented worked for me:
plot_ly(df, x = ~MediaDate, y = ~Spend, type = "bar", name = "Spend") %>%
add_trace(x = ~MediaDate, y = ~Visits, type = 'scatter', mode = "lines", yaxis = "y2", name = "Visits") %>%
layout(yaxis2 = list(overlaying = "y", side = "right"))