Home > Net >  How can I view the exact values I generated in my graph?
How can I view the exact values I generated in my graph?

Time:10-28

I have made a graph but I don't know how to view the exact values of the bars on the graph. Here is my code in case it is needed. I also have a picture of my graph.

Step 1: Load the tidyverse and tidyquant:
install.packages("tidyverse")
install.packages("tidyquant")
library("tidyverse")
library("tidyquant")

#STEP 2: Getting stocks data:
stocks <- c("TSLA", "UPST", "PLTR", "SPOT", "SHOP", "SPY", "BND")
stocks_df <- tq_get(stocks, from = '2017-01-01')

#Step 3: Group data:
port <- tq_get(c("TSLA", "UPST", "PLTR", "SPOT", "SHOP", "SPY", "BND"),
               from = '2017-01-01')%>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = "daily",
               col_rename = "ret")

#Step 4: Computing portfolio returns:
myport <- port %>% tq_portfolio(symbol,ret, c(0.2, 0.2, 0.2, 0.2, 0.2, 0, 0))
benchmark <- port %>% tq_portfolio(symbol, ret, c(0, 0, 0, 0, 0, 0.6, 0.4))

#Step 5: Computing portfolio measure:
mVaR <- myport %>% tq_performance(portfolio.returns,
                                  performance_fun = VaR,
                                  p = 0.95,
                                  method = "historical",
                                  portfolio_method = "single") %>%
  add_column(symbol = "MyPort", .before = 1)

bVaR <- benchmark %>% tq_performance(portfolio.returns,
                                     performance_fun = VaR,
                                     p = 0.95,
                                     method = "gaussian",
                                     portfolio_method = "single") %>%
  add_column(symbol = "Benchmark", .before = 1)

#Step 6: Computing portfolio measure: Expected Shortfall (ES):
mES <- myport %>% tq_performance(portfolio.returns,
                                 performance_fun = ES,
                                 p = 0.95,
                                 method = "historical",
                                 portfolio_method = "single") %>%
  add_column(symbol = "MyPort", .before = 1)

bES <- benchmark %>% tq_performance(portfolio.returns,
                                    performance_fun = ES,
                                    p = 0.95,
                                    method = "gaussian",
                                    portfolio_method = "single") %>%
  add_column(symbol = "Benchmark", .before = 1)

#Step 7: Combining the results into a single table using rbind (row bind):
bothVaR <- rbind(mVaR, bVaR)
bothES <- rbind(mES, bES)
results <- inner_join(bothVaR, bothES)

#Step 8: Re-shaping the table into a data frame suitable for plotting:
results <- results %>%
  pivot_longer(!symbol, names_to = "measure", values_to = "value")

#Step 9: Plot the results:
results %>% ggplot(aes(x = measure, y = abs(value), fill = symbol))  
  geom_bar(stat = "identity", position = "dodge")  
  labs(title = "Value at Risk Approach to Measure a Diversified Portfolio",
       x = "Risk Measure", y = " ", fill = " ")   theme_minimal()  
  theme(plot.title = element_text(hjust = 0.5), legend.position = "top")

enter image description here

I tried looking up on Google but the examples they give is for a specific set of data with different names and values. I don't know to implement it into my code for my specific script and graph.

CodePudding user response:

If you want to plot the values on the plot, this could work:

library(ggrepel)
results %>% ggplot(aes(x = measure, y = abs(value), fill = symbol))  
  geom_bar(stat = "identity", position = "dodge")  
  labs(title = "Value at Risk Approach to Measure a Diversified Portfolio",
       x = "Risk Measure", y = " ", fill = " ")   theme_minimal()  
  theme(plot.title = element_text(hjust = 0.5), legend.position = "top")  
  geom_text_repel(aes(label = round(abs(value), digits = 3)),
                  position = position_dodge(width = 1), direction = "y", size = 2.5)

Barplot with values as text repel

CodePudding user response:

One way to visualize the values when you hover over them is to use ggplotly as follow:

install.packages("plotly")
library(plotly)

Once you load the library, you store then your plot in a variable that I name

p

and do following:

plotly(p)
  • Related