I have a plot using plotly r . As of now the variations cant be seen that much. So in order to do so if the y-axis is 0-70% is there a way to show just 50%-70% on y axis so the variation can be seen more clearly.
Below is code I am using
output$plot <- renderPlotly({
if (is.null(ab()))
return(NULL)
y <- list(title = "Percentange")
x <- list(title = "Months")
plot_ly(ab(), x = ~ Month_considered, y = ~ pct * 100,type = 'scatter', mode = 'marker',
fill = 'tozeroy', line = list(color = 'rgb(205, 12, 24)', width = 4)) %>%layout(xaxis = x, yaxis = y)})
You don't provide a reproducible example so it is hard to test, but you could try:
y <- list(title = "Percentage", range = c(50,70))
plot_ly(ab(), x = ~ Month_considered, y = ~ pct * 100,type = 'scatter', mode = 'marker', fill = 'tozeroy', line = list(color = 'rgb(205, 12, 24)', width = 4)) %>% layout(xaxis = x, yaxis = y)})
Related
I have this data:
myData <- data.frame(Zupanija = c('GZ','ZGZ','KZ','VZ','KK','M','BB','VP','PS','BP','OB','VU','KA','SM','PG','LS','ZA','ŠK','SD','IS','DN'),
Inv = c(5205740135,2017069611,568884637,908563032,487561769,735161926,284291246,195329869,257799660,295494321,721957349,383617802,464253852,298576348,1182794616,277411269,677612459,405016102,3041655541,1039402830,642317513))
I want to do simple Plotly point graph and I would like to order data on y axis by the Inv variable (x axis) with this code:
myData %>%
plot_ly(x = ~Inv, y = ~ Zupanija) %>%
add_trace(type = 'scatter',
mode = 'markers',
stroke = I("black"),
span = I(1),
size= ~ sqrt(Inv),
color = ~ sqrt(Inv),
colors = inferno(50, alpha = 1, begin = 0, end = 1, direction = 1),
alpha = 1,
showlegend = FALSE)%>%
hide_colorbar()%>%
layout(xaxis = list(title = "HAMAG - ukupna ulaganja"),
yaxis = list(title=F, categoryorder = "total ascending"))
You can see that altough I put this part of the code layout(xaxis = list(title = "HAMAG - ukupna ulaganja"), yaxis = list(title=F, categoryorder = "total ascending")) - I do not get wanted result. Some variables are sorted but some variables on the y axis are not sorted based on x value: e.g. ZGZ (2B) is above GZ (5B) and BB is above KA, although it has smaller value on x. Does anyone see my mistake? tnx.
One option would be to manually arrange your dataset in your desired order and setting the categoryorder to "trace":
library(plotly)
library(viridis)
myData %>%
arrange(reorder(Zupanija, Inv)) %>%
plot_ly(x = ~Inv, y = ~Zupanija) %>%
add_trace(
type = "scatter",
mode = "markers",
stroke = I("black"),
span = I(1),
size = ~ sqrt(Inv),
color = ~ sqrt(Inv),
colors = inferno(50, alpha = 1, begin = 0, end = 1, direction = 1),
alpha = 1,
showlegend = FALSE
) %>%
hide_colorbar() %>%
layout(
xaxis = list(title = "HAMAG - ukupna ulaganja"),
yaxis = list(title = F, categoryorder = "trace")
)
I am plotting some plot with plotly. So everything is ok but I am not satisfied with font appearance. Namely in legend I have one font while for x and y-axis I have other. So can anybody help how to fix this problem and have same font like font in legend ?
x1 = lcBR$p
y1 = lcBR$L
x2 = lcAR$p
y2 = lcAR$L
fig <- plot_ly(x = ~ x1, y = ~ x1, name = '45º', type = 'scatter', mode = 'lines',
line = list(color = "black", width = 2))
fig <- fig %>% add_trace(x = ~ x1, y = ~ y1, name = 'Before reform', mode = 'lines',
line = list(color = "red", width = 2, dash = "solid"))
fig <- fig %>% add_trace(x ~ x2, y = ~ y2, name = 'After reform', mode = 'lines',
line = list(color = "blue", width = 2, dash = "dash"))
f <- list(
family = "Courier New, monospace",
size = 18,
color = "#7f7f7f")
x <- list(
title = "Share of Population",
titlefont = f)
y <- list(
title = "Share of income",
titlefont = f)
fig <- fig %>% layout(xaxis = x, yaxis = y)
fig
Your code looks a bit different than what "normal" python code in plotly looks like; is this R-code? However, you can use fig.update_layout to update the layout throughout your figure and fig.update_xaxes to change the appearance of the x-axis including text; have a look at https://plotly.com/python/figure-labels/#global-and-local-font-specification
I would like to add the regression line to my correlation scatter plot. Unfortunately this doesn't really work with plot_ly(). I've already tried some solutions from other posts in this forum, but it doesn't work.
My data frame looks like the following (only a smart part of it):
My code for the plot and the actual plot-output look like the following:
CorrelationPlot <- plot_ly(data = df.dataCorrelation, x = ~df.dataCorrelation$prod1,
y = ~df.dataCorrelation$prod2, type = 'scatter', mode = 'markers',
marker = list(size = 7, color = "#FF9999", line = list(color = "#CC0000", width = 2))) %>%
layout(title = "<b> Correlation Scatter Plot", xaxis = list(title = product1),
yaxis = list(title = product2), showlegend = FALSE)
What I want to have is something like this:
which I have produced with the ggscatter() function:
library(ggpubr)
ggscatter(df.dataCorrelation, x = "prod1", y = "prod2", color = "#CC0000", shape = 21, size = 2,
add = "reg.line", add.params = list(color = "#CC0000", size = 2), conf.int = TRUE,
cor.coef = TRUE, cor.method = "pearson", xlab = product1, ylab = product2)
HOW do I get the regression line with plot_ly()??
CODE EDITING:
CorrelationPlot <- plot_ly(data = df.dataCorrelation, x = ~df.dataCorrelation$prod1,
y = ~df.dataCorrelation$prod2, type = 'scatter', mode = 'markers',
marker = list(size = 7, color = "#FF9999",
line = list(color = "#CC0000", width = 2))) %>%
add_trace(x = ~df.dataCorrelation$fitted_values, mode = "lines", type = 'scatter',
line = list(color = "black")) %>%
layout(title = "<b> Correlation Scatter Plot", xaxis = list(title = product1),
yaxis = list(title = product2), showlegend = FALSE)
GIVES:
How do I get here a line for the regression line??
I don't think there's a ready function like ggscatter, most likely you have to do it manually, like first fitting the linear model and adding the values to the data.frame.
I made a data.frame that's like your data:
set.seed(111)
df.dataCorrelation = data.frame(prod1=runif(50,20,60))
df.dataCorrelation$prod2 = df.dataCorrelation$prod1 + rnorm(50,10,5)
fit = lm(prod2 ~ prod1,data=df.dataCorrelation)
fitdata = data.frame(prod1=20:60)
prediction = predict(fit,fitdata,se.fit=TRUE)
fitdata$fitted = prediction$fit
The upper and lower bounds of the line are simply 1.96* standard error of prediction:
fitdata$ymin = fitdata$fitted - 1.96*prediction$se.fit
fitdata$ymax = fitdata$fitted + 1.96*prediction$se.fit
We calculate correlation:
COR = cor.test(df.dataCorrelation$prod1,df.dataCorrelation$prod2)[c("estimate","p.value")]
COR_text = paste(c("R=","p="),signif(as.numeric(COR,3),3),collapse=" ")
And put it into plotly:
library(plotly)
df.dataCorrelation %>%
plot_ly(x = ~prod1) %>%
add_markers(x=~prod1, y = ~prod2) %>%
add_trace(data=fitdata,x= ~prod1, y = ~fitted,
mode = "lines",type="scatter",line=list(color="#8d93ab")) %>%
add_ribbons(data=fitdata, ymin = ~ ymin, ymax = ~ ymax,
line=list(color="#F1F3F8E6"),fillcolor ="#F1F3F880" ) %>%
layout(
showlegend = F,
annotations = list(x = 50, y = 50,
text = COR_text,showarrow =FALSE)
)
Another option is using ggplotly as
library(plotly)
ggplotly(
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length))+
geom_point(color = "#CC0000", shape = 21, size = 2) +
geom_smooth(method = 'lm') +
annotate("text", label=paste0("R = ", round(with(iris, cor.test(Sepal.Length, Petal.Length))$estimate, 2),
", p = ", with(iris, cor.test(Sepal.Length, Petal.Length))$p.value),
x = min(iris$Sepal.Length) + 1, y = max(iris$Petal.Length) + 1, color="steelblue", size=5)+
theme_classic()
)
I wrote a code to make a subplots with scatterplots using my data. Here is a graph:
This is hours on x axis. As you see, not all of them appear on x axis. How could i make all 24 hours be on axis? Even if for example in dataframe there is no value for 23 o'clock, i want it to be on x axis. How to do that?
Here is my code:
plot <- function(df) {
subplotList <- list()
for(metric in unique(df$metrics)){
subplotList[[metric]] <- df[df$metrics == metric,] %>%
plot_ly(
x = ~ hr,
y = ~ actual,
name = ~ paste(metrics, " - ", time_pos),
colors = ~ time_pos,
hoverinfo = "text",
hovertemplate = paste(
"<b>%{text}</b><br>",
"%{xaxis.title.text}: %{x:+.1f}<br>",
"%{yaxis.title.text}: %{y:+.1f}<br>",
"<extra></extra>"
),
type = "scatter",
mode = "lines+markers",
marker = list(
size = 7,
color = "white",
line = list(width = 1.5)
),
width = 700,
height = 620
) %>% layout(autosize = T,legend = list(font = list(size = 8)))
}
subplot(subplotList, nrows = length(subplotList), margin = 0.05)
}
This could be achieved in layout via the attribute xaxis like so. The ticks or breaks can be set via tickvals, the tick labels via ticktext.
This is illustrasted using some random data in the reproducible example below:
library(plotly)
set.seed(42)
d <- data.frame(
x = sort(sample(24, 15)),
y = 1:15 + runif(15),
z = 1:15 + runif(15)
)
plot_ly(d) %>%
add_trace(x = ~x, y = ~y, type = "scatter", mode = "lines+markers") %>%
add_trace(x = ~x, y = ~z, type = "scatter", mode = "lines+markers") %>%
layout(xaxis = list(tickvals = 1:24, ticktext = paste0(1:24, "h")))
When I try to modify my y axis title, it just disapears. Modifying the y axis ticks works just fine. Has anyone had this problem? Thanks!
library(plotly)
set.seed(2017)
x <- seq(1:10)
y <- x + rnorm(10)
plot_ly( x = ~x, y = ~y + rnorm(10)) %>%
layout(
xaxis = list(tickfont = list(size = 15)),
yaxis = list(tickfont = list(size = 25))) ## This works well.
plot_ly( x = ~x, y = ~y + rnorm(10)) %>%
layout(
xaxis = list(tickfont = list(size = 15)),
yaxis = list(titlefont = list(size = 25))) ## This makes the y axis label disappear.
The goal is to modify the size of the y axis title, not to make it disappear all together.
It seems that it those cases it is necessary to also specify the title itself:
plot_ly( x = ~x, y = ~y + rnorm(10)) %>%
layout(
xaxis = list(tickfont = list(size = 15)),
yaxis = list(titlefont = list(size = 25), title = "test"))
In case someone is looking for similar answer for python.
You can add fig.update_yaxes(title_font=dict(size=12))