I'm learning how to write code for different kinds of charts in R using plotly library. For example:
library(plotly)
library(quantmod)
getSymbols("AAPL",src='yahoo')
# basic example of ohlc charts
df <- data.frame(Date=index(AAPL),coredata(AAPL))
df <- tail(df, 30)
maxi_dif <- as.integer(which (df$diff == max(df$diff)))
maxi_x <- df[maxi_dif,'Date']
maxi_y <- df[maxi_dif,'diff']
fig <- df %>% plot_ly(x = ~Date, type="candlestick",
open = ~AAPL.Open, close = ~AAPL.Close,
high = ~AAPL.High, low = ~AAPL.Low)
There are different kinds of options of how to add legend,hovertext and etc on the plot. I wonder if it's possible to highlight certain candle in the chart? For example based on what candle is the longest/shortest in the data? I'm not very experienced in how these charts work, I only know the basics, so maybe the question is not applicable.
Example of what I want to achieve is in the attached image
image
Add a new trace with filtered data:
fig <- df %>% plot_ly(
x = ~ Date,
type = "candlestick",
open = ~ AAPL.Open,
close = ~ AAPL.Close,
high = ~ AAPL.High,
low = ~ AAPL.Low
) %>%
add_trace(
data = df %>% filter(Date == "2022-01-28"),
x = ~ Date,
type = "candlestick",
open = ~ AAPL.Open,
close = ~ AAPL.Close,
high = ~ AAPL.High,
low = ~ AAPL.Low,
increasing = list(fillcolor = "Blue", line = list(color = "Blue")),
decreasing = list(fillcolors = "Blue", line = list(color = "Blue")),
showlegend = FALSE
)
fig
Related
I know there are a few similar questions to this out there but they all seem to use javascript (?) or something besides the normal R coding so I don't know how to use it in my code... anyways all I want to do is add a plotline to my area chart that shows the average of the values, how do I do that? I know that highcharter itself can not calculate the average so I can do that myself but how do I create the plotline .... thank you so much. (i tried to make the code so that it is easily 'reproducible' ? hope it is ok). I attached a picture of the current chart if that helps.
library(tidyverse)
library(highcharter)
library(ggplot2)
data("diamonds", package = "ggplot2")
df <- diamonds %>%
group_by(cut)%>%
count()
head(df, 4)
# Create chart
hc <- df %>%
hchart(
'area', hcaes(x = cut, y = n),
color = "lightblue"
) %>%
hc_yAxis(title = list(text = "cut"))
# Display chart
hc
Below is a mini example of using the highcharts widget. You can add each series using hc_add_series. In this case, we have two series and two y-axes. Using two y-axes helps to differentiate between the series. I'm not sure what values you're trying to calculate the average so I chose price.
Hope this helps add some clarity to highcharter!
library(tidyverse)
library(highcharter)
df <- diamonds %>%
group_by(cut)%>%
summarise(
n = n(),
avg_price = round(mean(price),2)
)
# create hc widget
highchart(type = "chart") %>%
# add both series
hc_add_series(df, hcaes(x = cut, y = n), color = "lightblue", yAxis = 0, type = "area", name = "N") %>%
hc_add_series(df, hcaes(x = cut, y = avg_price), yAxis = 1, type = "line", name = "Avg Price") %>%
# set type to categories since we're looking at categorical data
hc_xAxis(type = "category", categories = df$cut) %>%
hc_title(text = "Cut Freq vs Avg Price") %>%
# add each y-axis which is linked above in 'hc_add_series'
hc_yAxis_multiples(
list(title = list(text = "Cut")), # yAxis = 0
list(title = list(text = "Average Price"), opposite = TRUE) # yAxis = 1
) %>%
hc_tooltip(shared = TRUE, split = FALSE)
Ex:
Haha I got it. basically just this.
plotline <- list(
color = "red", value = mean(diamonds$cut), width = 2, zIndex = 5
)
hc_yAxis(plotLines = list(plotline))
I need to plot a line chart that has more than 50 different lines. I am using RColorBrewer to generate 50 unique colors to plot it. However some of the generated colors are very hard to read (e.g. bright yellow). Is there anyway to generate a set of colors that are easier to read?
library(tidyverse)
library(RColorBrewer)
library(plotly)
colors_ = colorRampPalette(brewer.pal(10,"Spectral"))(50)
grps = sort(rep(c(1:50),2)) %>% as.character() %>% unique
dat = data.frame(x = rep(c(1,2),50) , y = sort(rep(c(1:50),2)) , grp =as.character(sort(rep(c(1:50),2))) )
p = plot_ly(dat ,type = 'scatter', mode = 'lines')
for (i in 1:50){
p <- p %>% add_trace(data = filter(dat , grp ==grps[i] ) , x = ~x, y=~y,
line = list(color = colors_[i]),
text = grps[i]
)
}
p
I probably have a simple question but I can't find a way to achieve what I need. I have a simple boxplot as the following:
end_dt <- as.Date("2021-02-12")
start_dt <- end_dt - (nrow(iris) - 1)
dim(iris)
dates <- seq.Date(start_dt, end_dt, by="1 day")
df <- iris
df$LAST_VAL <- "N"
df[3, 'LAST_VAL'] <- "Y"
df1 <- df[,c("Sepal.Length","LAST_VAL")]
df1$DES <- 'Sepal.Length'
colnames(df1) <- c("VALUES","LAST_VAL","DES")
df2 <- df[,c("Sepal.Width","LAST_VAL")]
df2$DES <- 'Sepal.Width'
colnames(df2) <- c("VALUES","LAST_VAL","DES")
df <- rbind(df1, df2)
fig <- plot_ly(df, y = ~VALUES, color = ~DES, type = "box") %>% layout(showlegend = FALSE)
What I would like to do now is a add a red marker to each box plot just for the value corresponding to LAST_VAL = "Y". This would allow me to see given the distribution of each plot, to see where the most recent value is located.
I tried to use the info on https://plotly.com/r/box-plots/ but I can't figure out how to do this.
Thanks
The following solution ended up to be a bit too long codewise. However, it should give you what you asked for. I think the boxplots should be added afterwards, like:
fig <- plot_ly(df[df$LAST_VAL=="Y",],
x=~DES, y = ~VALUES, color = ~DES, type = "scatter", colors='red') %>%
layout(showlegend = FALSE) %>%
add_boxplot(data = df[df$DES=="Sepal.Length",], x = ~DES, y = ~VALUES,
showlegend = F, color = ~DES,
boxpoints = F, fillcolor = 'white', line = list(color = c('blue'))) %>%
add_boxplot(data = df[df$DES=="Sepal.Width",], x = ~DES, y = ~VALUES,
showlegend = F, color = ~DES,
boxpoints = F, fillcolor = 'white', line = list(color = c('green')))
I am making a pie-chart in plotly in R.
I want my labels to be on the chart, so I use textposition = "inside", and for the very small slices those values are not visible.
I am trying to find a way to exclude those labels.
Ideally, I would like to like to not print any lables on my plot that are below 10%.
Setting textposition = "auto" doesn't work well, since there are a lot of small slices, and it makes the graph look very messy.
Is there a way to do it?
For example these piecharts from plotly website (https://plot.ly/r/pie-charts/)
library(plotly)
library(dplyr)
cut <- diamonds %>%
group_by(cut) %>%
summarize(count = n())
color <- diamonds %>%
group_by(color) %>%
summarize(count = n())
clarity <- diamonds %>%
group_by(clarity) %>%
summarize(count = n())
plot_ly(cut, labels = cut, values = count, type = "pie", domain = list(x = c(0, 0.4), y = c(0.4, 1)),
name = "Cut", showlegend = F) %>%
add_trace(data = color, labels = color, values = count, type = "pie", domain = list(x = c(0.6, 1), y = c(0.4, 1)),
name = "Color", showlegend = F) %>%
add_trace(data = clarity, labels = clarity, values = count, type = "pie", domain = list(x = c(0.25, 0.75), y = c(0, 0.6)),
name = "Clarity", showlegend = F) %>%
layout(title = "Pie Charts with Subplots")
In the plot for Clarity 1.37% are outside of the plot, while I would like them not to show at all.
You'll have to specify sector labels manually like so:
# Sample data
df <- data.frame(category = LETTERS[1:10],
value = sample(1:50, size = 10))
# Create sector labels
pct <- round(df$value/sum(df$value),2)
pct[pct<0.1] <- 0 # Anything less than 10% should be blank
pct <- paste0(pct*100, "%")
pct[grep("0%", pct)] <- ""
# Install devtools
install.packages("devtools")
# Install latest version of plotly from github
devtools::install_github("ropensci/plotly")
# Plot
library(plotly)
plot_ly(df,
labels = ~category, # Note formula since plotly 4.0
values = ~value, # Note formula since plotly 4.0
type = "pie",
text = pct, # Manually specify sector labels
textposition = "inside",
textinfo = "text" # Ensure plotly only shows our labels and nothing else
)
Check out https://plot.ly/r/reference/#pie for more information...
I'm trying to draw a time series boxplot in R with the plotly libraries, however I need to be able to have total control of the ymin, ymax, ylow etc.
It renders perfectly fine in ggplot2, althought with a ton of warnings. It fails to render in ggplotly
Here is what I have.
msft = read.csv("http://ichart.finance.yahoo.com/table.csv?s=MSFT",
header=TRUE,
sep=",")
msft$Date
msftF = msft %>% tbl_df() %>% filter(as.Date(Date) > as.Date("2016-01-01")) %>% na.omit()
msftF %>%
ggplot(aes(x = factor(Date), ymin = Low, lower = Open, middle = Close, upper = Close, ymax = High)) +
geom_boxplot() +
geom_boxplot(stat = "identity")
#David Crook here's a simple example.
library(plotly)
library(quantmod)
prices <- getSymbols("MSFT", auto.assign = F)
prices <- prices[index(prices) >= "2016-01-01"]
# Make dataframe
prices <- data.frame(time = index(prices),
open = as.numeric(prices[,1]),
high = as.numeric(prices[,2]),
low = as.numeric(prices[,3]),
close = as.numeric(prices[,4]))
# Blank plot
p <- plot_ly()
# Add high / low as a line segment
# Add open close as a separate segment
for(i in 1:nrow(prices)){
p <- add_trace(p, data = prices[i,], x = c(time, time), y = c(high, low), mode = "lines", evaluate = T,
showlegend = F,
marker = list(color = "grey"),
line = list(width = 1))
p <- add_trace(p, data = prices[i,], x = c(time, time), y = c(open, close), mode = "lines", evaluate = T,
showlegend = F,
marker = list(color = "#ff5050"),
line = list(width = 5))
}
p
UPDATE:
with the release of plotly 4.0 doing this is a lot easier:
library(plotly)
library(quantmod)
prices <- getSymbols("MSFT", auto.assign = F)
prices <- prices[index(prices) >= "2016-01-01"]
# Make dataframe
prices <- data.frame(time = index(prices),
open = as.numeric(prices[,1]),
high = as.numeric(prices[,2]),
low = as.numeric(prices[,3]),
close = as.numeric(prices[,4]))
plot_ly(prices, x = ~time, xend = ~time, showlegend = F) %>%
add_segments(y = ~low, yend = ~high, line = list(color = "gray")) %>%
add_segments(y = ~open, yend = ~close,
color = ~close > open,
colors = c("#00b386","#ff6666"),
line = list(width = 3))
For a more complete example see here: http://moderndata.plot.ly/candlestick-charts-using-plotly-and-quantmod/
You can do this using the quantmod package.
Try the following:
library(quantmod)
getSymbols("MSFT")
candleChart(MSFT,multi.col=TRUE,theme='white')
You can trim the MSFT object to a smaller date range if you don't need all of them.
If you need to do it with ggplot, let me know and I'll write up the code. But typically, I stick with the packages where I can as it's cleaner!
Royr2 answered this, however he has not moved the answer from comments into answers in a few days, so to capture the answer as a marked answer, I am simply migrating his comment into answers. If he posts his answer, I will gladly mark his answer as the appropriate answer.
"
I wrote something for the folks at plotly here -> http://moderndata.plot.ly/candlestick-charts-using-plotly-and-quantmod/ Uses plot_ly() instead of ggplotly(). Goes without saying that the function is inspired by charts in the quantmod package :) Hopefully helps... –
"
royr2 Apr 25 at 5:02