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 have made twoplots using plotly, which are working fine individually, but when combined using subplot I can't seem to figure out how to combine the legends. I have tried to use showlegend = F in plot_ly in one of the plots, but this just removes it completely - what I want is to control both subplots with the same legend.
My code is as follows:
coronavirus_not_china <- coronavirus %>%
filter(!(country == "China"))
cases_not_china_plot <- coronavirus_not_china %>%
group_by(type, date) %>%
summarise(total_cases = sum(cases)) %>%
pivot_wider(names_from = type, values_from = total_cases) %>%
arrange(date) %>%
mutate(active = confirmed - death - recovered) %>%
mutate(active_total = cumsum(active),
recovered_total = cumsum(recovered),
death_total = cumsum(death)) %>%
plot_ly(x = ~ date,
y = ~ active_total,
name = 'Active',
fillcolor = '#1f77b4',
type = 'scatter',
mode = 'none',
stackgroup = 'one',
showlegend = F) %>%
add_trace(y = ~ death_total,
name = "Death",
fillcolor = '#E41317') %>%
add_trace(y = ~recovered_total,
name = 'Recovered',
fillcolor = 'forestgreen') %>%
layout(title = "Distribution of Covid19 Cases outside China",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Number of Cases", showgrid = T))
coronavirus_china <- coronavirus %>%
filter((country == "China"))
cases_china_plot <- coronavirus_china %>%
group_by(type, date) %>%
summarise(total_cases = sum(cases)) %>%
pivot_wider(names_from = type, values_from = total_cases) %>%
arrange(date) %>%
mutate(active = confirmed - death - recovered) %>%
mutate(active_total = cumsum(active),
recovered_total = cumsum(recovered),
death_total = cumsum(death)) %>%
plot_ly(x = ~ date,
y = ~ active_total,
name = 'Active',
fillcolor = '#1f77b4',
type = 'scatter',
mode = 'none',
stackgroup = 'one',
showlegend = T) %>%
add_trace(y = ~ death_total,
name = "Death",
fillcolor = '#E41317') %>%
add_trace(y = ~recovered_total,
name = 'Recovered',
fillcolor = 'forestgreen') %>%
layout(title = "Distribution of Covid19 Cases inside China",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Number of Cases", showgrid = F))
And I create the subplots as:
subplot(cases_not_china_plot, cases_china_plot, nrows = 2, margin = 0.05, shareX = T) %>%
layout(title="Coronavirus cases outside China and in China", ylab("Number of cases"))
I am quite new to R, so if there is a smarter way to do what I desire, please let me know.
With the above code, my output is:
I would like to localize a plotly graph date axis date format for a special country. Instead of labeling dates in the time axis as "Jan 8 Jan 9 Jan 10...", I want to label them "Oca 8 Oca 9 Oca 10" in Turkish locale. The sample plotly code in R is given below. I know that I need to use config(locale = "tr_TR") to do this. But I am not sure how to do this in the code below.
plotly::plot_ly() %>%
plotly::add_trace(
x = ~date,
y = ~Belgium,
type = "scatter",
mode = "lines+markers",
name = "Türkiye"
) %>%
plotly::add_trace(
x = ~date,
y = ~France,
type = "scatter",
mode = "lines+markers",
name = "Fransa"
) %>%
plotly::add_trace(
x = ~date,
y = ~Spain,
type = "scatter",
mode = "lines+markers",
name = "İspanya"
) %>%
plotly::add_trace(
x = ~date,
y = ~Italy,
type = "scatter",
mode = "lines+markers",
name = "Italya"
) %>%
plotly::layout(
title = "",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Toplam Onaylı Vakalar"),
xaxis = list(title = "Date"),
# paper_bgcolor = "black",
# plot_bgcolor = "black",
# font = list(color = 'white'),
hovermode = "compare",
margin = list(
# l = 60,
# r = 40,
b = 10,
t = 10,
pad = 2
)
)```
I just added the following
)%>%
plotly::config(
locale='tr')
)
This configuration sets the date axis labeling based on system locale.
I am trying to plot a chart where the user can select which column data they want to view in the plot. So if you see in the data below I want to plot a chart where the user can see the numbers by date for differnt fruits . So x axis should be the date, y axis should me either market1 , market2 or market3 which ever the user select and a area chart where the areas as split by fruit type.
test_data <-data.frame(
stringsAsFactors = FALSE,
market1 = c(0L,0L,1L,0L,0L,1L,1L,0L,
2L,2L,0L,0L,1L,0L,8L,8L,0L,4L,3L,0L,4L,2L,
0L,1L,2L,0L,3L,2L,0L,5L,0L,7L,1L,8L,4L,0L,
16L,22L,0L,35L,17L,57L,19L,0L,125L,15L,0L,49L,
18L,0L),
market2 = c(34L,5L,71L,3L,3L,30L,54L,
0L,15L,28L,0L,24L,53L,0L,169L,46L,0L,134L,40L,
0L,123L,18L,2L,54L,72L,0L,131L,44L,0L,111L,
35L,194L,19L,182L,65L,0L,462L,313L,0L,524L,300L,
1366L,216L,0L,2165L,297L,0L,1188L,196L,0L),
market3 = c(1762L,1139L,2562L,294L,235L,
890L,1619L,1L,453L,1434L,6L,872L,1933L,12L,
3951L,1563L,20L,2934L,2358L,126L,5182L,2264L,208L,
3897L,2735L,3L,3156L,2727L,8L,3667L,1273L,4819L,
679L,5470L,1829L,7L,9847L,7309L,30L,9235L,7933L,
30486L,4551L,2L,35029L,7018L,3L,25591L,3516L,1L),
fruit = c("Apple",
"Apple","Apple",
"Apple","Apple","Apple",
"Apple","Apple",
"Apple","Apple","Apple",
"Apple","Banana",
"Banana","Banana","Banana",
"Banana","Banana",
"Banana","Banana","Banana",
"Banana","Banana",
"Banana","Banana","Banana",
"Orange","Orange",
"Orange","Orange","Orange",
"Orange","Orange",
"Orange","Orange","Orange",
"Orange","Orange",
"Orange","Berries","Berries",
"Berries","Berries",
"Berries","Berries","Berries",
"Berries","Berries",
"Berries","Berries"),
date = as.factor(c("1/10/20",
"1/10/20","1/10/20","1/10/20","1/10/20",
"1/10/20","1/11/20","1/11/20","1/11/20",
"1/11/20","1/11/20","1/11/20","1/12/20","1/12/20",
"1/12/20","1/12/20","1/12/20","1/12/20",
"1/13/20","1/13/20","1/13/20","1/13/20",
"1/13/20","1/13/20","1/14/20","1/14/20",
"1/14/20","1/14/20","1/14/20","1/14/20","1/15/20",
"1/15/20","1/15/20","1/15/20","1/16/20",
"1/16/20","1/16/20","1/16/20","1/16/20",
"1/16/20","1/17/20","1/17/20","1/17/20",
"1/17/20","1/17/20","1/18/20","1/18/20","1/18/20",
"1/18/20","1/18/20"))
)
I tried doing it for just one market but I am unable to split it by fruit. How can I go about it.Below is the code for the plot.
plot_ly(test_data, x = test_data$date, y = test_data$market1, name = 'Tastemade', type = 'scatter', mode = 'none', stackgroup = 'one', fillcolor = '#F5FF8D')
layout(title = 'Conversions By Day',
# xaxis = list(title = 'Date',rangeslider = list(type = "date")),
xaxis = list(title = 'Date'),
yaxis = list(title = 'Conversions'))
I don't think you can create a plot where the user will be allowed to change the y-axis variable, unless you create a shiny app.
Here is an example of a scatter plot for market1, where the markers are coloured by fruit:
plot_ly(data = test_data, x = ~date, y = ~market1, color = ~fruit, colors = "RdYlGn",
type = 'scatter', mode = 'markers') %>%
layout(title = 'Conversions By Day',
# xaxis = list(title = 'Date',rangeslider = list(type = "date")),
xaxis = list(title = 'Date'),
yaxis = list(title = 'Conversions'))
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