Radar plot in R Shiny - r
I'm really new to R Shiny (starting playing with it today!), but this code isn't working for me... R keeps saying "the data must be given as dataframe." which, as far as I can tell, it is a dataframe (and it says it is when I check with is.data.frame).
# Load packages ----
library(shiny)
library(fmsb)
# Load data ----
industry <- read.csv("data/industry.csv")
# User interface ----
ui <- fluidPage(
titlePanel("L&D Capabilities 2023"),
sidebarLayout(
sidebarPanel(
helpText("Check which L&D capabilities your industry
has in-house in 2023."),
selectInput("var",
label = "Choose a variable to display",
choices = c("Central government",
"Local government",
"IT and Telecoms",
"Professional services, law and accountancy",
"Finance, banking and insurance",
"Health",
"Social care/housing association",
"Other charity/voluntary sector",
"Retail",
"Engineering",
"Manufacturing",
"Pharmaceutical",
"Transport",
"Utilities",
"Hospitality",
"Education (HE, FE)",
"Art, media and design",
"Other",
"Consulting"),
selected = "Central government"),
),
mainPanel(plotOutput("radarPlot"))
)
)
# Server logic ----
server <- function(input, output) {
output$radarPlot <- renderPlot({
data <- switch(input$var,
"Central government" = industry$Centralgov,
"Local government" = industry$Localgov,
"IT and Telecoms" = industry$IT,
"Professional services, law and accountancy" = industry$PS,
"Finance, banking and insurance" = industry$Finance,
"Health" = industry$Health,
"Social care/housing association" = industry$Social,
"Other charity/voluntary sector" = industry$Charity,
"Retail" = industry$Retail,
"Engineering" = industry$Engineering,
"Manufacturing" = industry$Manufacturing,
"Pharmaceutical" = industry$Pharmaceutical,
"Transport" = industry$Transport,
"Utilities" = industry$Utilities,
"Hospitality" = industry$Hospitality,
"Education (HE, FE)" = industry$Education,
"Consulting" = industry$Consulting,
"Art, media and design" = industry$Art,
"Other" = counties$Other)
radarchart(data)
})
}
# Run app ----
shinyApp(ui, server)
Any ideas what's going on? Or what I'm missing?
Many thanks!
EDIT: here's my data
> dput(industry)
structure(list(Max = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Min = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Centralgov = c(0.6,
0.18, 0.27, 0.27, 0.27, 0.27, 0.36, 0.3, 0.55, 0.45, 0.1, 0,
0.1, 0.27, 0.64, 0.09, 0.09, 0.18, 0.27, 0, 0.09, 0.18, 0.25,
0.29, 0.14), Localgov = c(0.36, 0.5, 0.36, 0.5, 0.42, 0.42, 0.09,
0.27, 0.36, 0.55, 0.3, 0.36, 0.55, 0.45, 0.73, 0.36, 0.18, 0.45,
0.64, 0.36, 0.27, 0.18, 0.3, 0.2, 0.6), IT = c(0.73, 0.33, 0.47,
0.51, 0.38, 0.18, 0.34, 0.38, 0.62, 0.41, 0.19, 0.38, 0.49, 0.41,
0.62, 0.32, 0.22, 0.38, 0.58, 0.51, 0.33, 0.34, 0.41, 0.15, 0.37
), PS = c(0.73, 0.4, 0.56, 0.6, 0.48, 0.48, 0.29, 0.24, 0.63,
0.56, 0.29, 0.41, 0.27, 0.36, 0.71, 0.28, 0.16, 0.48, 0.4, 0.52,
0.36, 0.38, 0.29, 0.25, 0.13), Finance = c(0.9, 0.44, 0.66, 0.66,
0.61, 0.52, 0.44, 0.5, 0.86, 0.62, 0.32, 0.39, 0.48, 0.59, 0.86,
0.3, 0.27, 0.5, 0.52, 0.52, 0.57, 0.51, 0.56, 0.33, 0.29), Health = c(0.88,
0.33, 0.47, 0.65, 0.28, 0.37, 0.33, 0.29, 0.78, 0.47, 0.18, 0.13,
0.47, 0.5, 0.78, 0.26, 0.16, 0.41, 0.58, 0.5, 0.38, 0.39, 0.33,
0.13, 0.29), Social = c(0.7, 0.25, 0.5, 0.33, 0.3, 0.2, 0.1,
0.4, 0.5, 0.2, 0, 0.22, 0, 0.2, 0.4, 0.1, 0.3, 0.1, 0.3, 0.3,
0.33, 0.3, 0.33, 0, 0.11), Charity = c(0.8, 0.55, 0.62, 0.44,
0.5, 0.31, 0.08, 0.33, 0.58, 0.5, 0.4, 0.36, 0.33, 0.38, 0.82,
0.15, 0.08, 0.36, 0.22, 0.42, 0.2, 0.42, 0.18, 0.22, 0.11), Retail = c(0.62,
0.38, 0.46, 0.27, 0.25, 0.09, 0.08, 0.31, 0.82, 0.46, 0.25, 0.27,
0.25, 0.54, 0.69, 0.08, 0.17, 0.31, 0.67, 0.5, 0.33, 0.5, 0.38,
0.18, 0.08), Engineering = c(0.6, 0, 0.4, 0.25, 0.17, 0.17, 0,
0, 0.33, 0.5, 0.25, 0.33, 0.6, 0.17, 0.33, 0, 0, 0.33, 0.33,
0.17, 0.17, 0.5, 0.2, 0, 0), Manufacturing = c(0.56, 0.22, 0.35,
0.42, 0.42, 0.4, 0.24, 0.2, 0.56, 0.41, 0.24, 0.11, 0.21, 0.3,
0.63, 0.1, 0, 0.25, 0.42, 0.58, 0.21, 0.35, 0.25, 0.33, 0.06),
Pharmaceutical = c(0.43, 0.25, 0, 0.71, 0.63, 0.25, 0.13,
0.13, 0.63, 0.43, 0, 0, 0, 0, 0.38, 0.25, 0.13, 0.38, 0.38,
0.5, 0, 0, 0.33, 0, 0.17), Transport = c(0.77, 0.62, 0.79,
0.57, 0.71, 0.64, 0.14, 0.5, 0.79, 0.46, 0.38, 0.21, 0.36,
0.38, 0.64, 0.43, 0.29, 0.21, 0.57, 0.64, 0.29, 0.54, 0.57,
0.36, 0.15), Utilities = c(1, 0.6, 0.4, 0.33, 0.2, 0.2, 0.6,
0.6, 0.8, 0.6, 0.25, 0.2, 0.8, 0.4, 1, 0.4, 0.4, 0.6, 0.4,
0.6, 0.2, 0.2, 0.2, 0.2, 0), Hospitality = c(0.67, 0, 0.67,
0.4, 0.67, 0.33, 0.33, 0.83, 0.83, 0.2, 0.67, 0.17, 0.2,
0.33, 0.83, 0.33, 0.33, 0, 0.67, 1, 0.5, 0.33, 0.33, 0.6,
0.33), Education = c(0.87, 0.33, 0.47, 0.53, 0.41, 0.38,
0.5, 0.47, 0.65, 0.41, 0.2, 0.31, 0.47, 0.65, 0.53, 0.24,
0.29, 0.38, 0.56, 0.41, 0.31, 0.19, 0.38, 0.27, 0.35), Consulting = c(0.67,
0.5, 0.67, 1, 0.33, 0.33, 0.17, 0.5, 1, 0.6, 0.33, 0.6, 0.4,
0.67, 0.5, 0.17, 0.17, 0.4, 0.6, 0.5, 0.5, 0.33, 0.4, 0.25,
0.25), Art = c(1, 0.2, 0.6, 0.5, 0.4, 0.2, 0.2, 0.6, 0.6,
0.4, 0.2, 0.2, 0.6, 0.5, 0.5, 0.2, 0.2, 0.2, 0.2, 0.6, 0.4,
0.4, 0.4, 0.2, 0.25), Other = c(0.67, 0.57, 0.71, 0.29, 0.57,
0.43, 0.14, 0.5, 0.67, 0.29, 0.57, 0.29, 0.43, 0.57, 0.71,
0.29, 0.43, 0.29, 0.43, 0.57, 0.71, 0.43, 0.5, 0.6, 0.4)), class = "data.frame", row.names = c("In-person classroom delivery",
"Strategy and governance", "Stakeholder engagement", "Instructional design",
"Crafting learning journeys / blended solutions", "Supporting ongoing workplace performance",
"Facilitating social and collaborative learning", "Understanding learner behaviour",
"Virtual classroom / webinar delivery", "Digital content development",
"Performance consulting", "Business acumen", "Marketing and communications",
"Coaching and mentoring", "Learning management / administration",
"Analytics / data management", "Evaluating impact", "Technology/infrastructure",
"Project management", "Leveraging L&D expertise", "Knowledge management",
"Negotiation, persuasion, and influence", "Learning experience design",
"Community engagement", "Research capabilities"))
Like I mentioned in my comment, radarchart requires at least three variables.
In this solution, I set the plot to only render when there are at least three options selected. I've changed the creation of the dropdown to allow multiple selections (so that three can be selected.
ui <- fluidPage(
titlePanel("Title"),
sidebarLayout(sidebarPanel(
helpText("help goes here"),
selectInput(
"var", label = "Choose at least three options to plot",
multiple = T,
choices = setNames(
names(industry)[3:21],
c("Central government", "Local government", "IT and Telecoms",
"Professional services, law and accountancy",
"Finance, banking and insurance", "Health",
"Social care/housing association", "Other charity/voluntary sector",
"Retail", "Engineering","Manufacturing", "Pharmaceutical", "Transport",
"Utilities", "Hospitality", "Education (HE, FE)", "Consulting",
"Art, media and design", "Other")), selected = "Central government")
), mainPanel(plotOutput("radarPlot")))
)
server <- function(input, output, session) {
output$radarPlot <- renderPlot({
if(length(input$var) >= 3) {
radarchart(industry[, input$var])
}
})
}
# Run app ----
shinyApp(ui, server)
However, this is not technically accurate since the first row is supposed to be the max value. The second row is supposed to be the min value. (Which is how your data frame is orientated if you transpose it.)
Here's another version of the server. In this version, I've modified the data so that the first and second rows are the max and min.
# add max and min to the top of every column
industry2 <- t(industry) %>%
as.data.frame() %>%
mutate(max = 1, min = 0) %>%
select(max, min, everything()) %>%
t() %>% as.data.frame()
server2 <- function(input, output, session) {
output$radarPlot <- renderPlot({
if(length(input$var) >= 3) {
radarchart(industry[, input$var])
}
})
}
# Run app ----
shinyApp(ui, server2)
This renders an identical plot to what I pictured for the first version (so I am not sure why the radarchart function calls for this information.)
I'm not sure how attached you are to fmsb::radarchart, but there are a lot of other options: ggplot2, plotly, highcharter, echarts, etc.
Here's an alternative using the Plotly library. In this option, I've set multiple in selectInput to false (based on what you were originally trying to plot). I've set the range so the graphs are comparable. A few other changes are annotated in the comments in the code.
library(plotly)
library(shiny)
ui2 <- fluidPage(
titlePanel("With Plotly"),
sidebarLayout(sidebarPanel(
helpText("help goes here"),
selectInput(
"var", label = "Choose a field to plot",
multiple = F, # <<- FALSE now (can remove argument, default is false)
choices = setNames(
names(industry)[3:21],
c("Central government", "Local government", "IT and Telecoms",
"Professional services, law and accountancy",
"Finance, banking and insurance", "Health",
"Social care/housing association", "Other charity/voluntary sector",
"Retail", "Engineering","Manufacturing", "Pharmaceutical", "Transport",
"Utilities", "Hospitality", "Education (HE, FE)", "Consulting",
"Art, media and design", "Other")), selected = "Central government")
), mainPanel(plotlyOutput("radarPlot"))) # <<-- plotlyOutput instead of plotOutput
)
server3 <- function(input, output, session) {
output$radarPlot <- renderPlotly({ # <- render plotly instead of plot
plot_ly(r = industry[, input$var], theta = rownames(industry),
fill = "toself", type = "scatterpolar", mode = "markers") %>%
layout(
polar = list(
angularaxis = list(showticklabels = F),
radialaxis = list(range = c(0, 1))
))
})
}
# Run app ----
shinyApp(ui2, server3)
I hid the labels, because they overlapped. However, when you hover, you can see the row names.
Related
Raytracing many objects and exporting shaded ground as a raster
I have models consisting of thousands of cylinders which are stored in a data frame with start-coordinates, end-coordinates, lengths and radii. I want to simulate the shading they would create in the real world, given a certain position of a light source. As a result, I would like to have a raster which contains the information whether the ground is shaded or not (on the xy-plane). Is there a way to do this in R? Even when handling several thousand cylinder objects at the same time? Here a mockup for a single cylinder: I usually draw my cylinders with the rgl package, but it would be also okay if I have to use another package. I figured I might be able to use the packages rayrender or raytracing, but I don't know to export the shaded ground from the view to an array or raster. Edit: Code for creating & plotting some cylinders: library(rgl) # some cylinders cylinder <- structure(list(radius = c(0.01, 0.01, 0.01, 0.02, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01), length = c(0.07, 0.13, 0.08, 0.08, 0.1, 0.08, 0.09, 0.08, 0.07, 0.15, 0.02, 0.09, 0.12, 0.12, 0.08, 0.26, 0.1, 0.09, 0.08, 0.02, 0.12, 0.11, 0.08, 0.06, 0.06, 0.19, 0.05, 0.1, 0.09, 0.09), start_X = c(0.62, 0.61, 0.62, 0.63, 0.64, 0.65, 0.65, 0.63, 0.63, 0.63, 0.63, 0.64, 0.63, 0.69, 0.79, 0.81, 0.92, 0.97, 1.03, 1.07, 1.08, 1.15, 1.24, 1.3, 1.34, 0.61, 0.5, 0.47, 0.4, 0.37), start_Y = c(0.13, 0.11, 0.09, 0.09, 0.09, 0.08, 0.09, 0.08, 0.07, 0.08, 0.07, 0.07, 0.07, 0.05, 0.02, 0.04, 0.02, 0.01, 0.04, 0.05, 0.05, 0.1, 0.15, 0.19, 0.22, 0.07, 0.13, 0.16, 0.17, 0.26), start_Z = c(361, 361.07, 361.2, 361.29, 361.36, 361.46, 361.54, 361.62, 361.7, 361.77, 361.9, 361.92, 361.78, 361.88, 361.98, 362.04, 362.26, 362.35, 362.39, 362.46, 362.48, 362.56, 362.62, 362.65, 362.66, 361.76, 361.91, 361.95, 362.01, 362.07 ), axis_X = c(-0.09, 0.05, 0.12, 0.14, 0.1, -0.03, -0.15, -0.07, -0.07, -0.2, 0.52, -0.62, 0.43, 0.54, 0.16, 0.35, 0.53, 0.43, 0.76, 0.58, 0.63, 0.74, 0.66, 0.56, 0.79, -0.61, -0.65, -0.64, -0.33, -0.7), axis_Y = c(-0.12, -0.09, -0.01, -0.08, -0.08, 0.01, -0.11, -0.14, -0.04, -0.06, 0.06, 0.59, -0.14, -0.14, 0.38, -0.22, 0.1, 0, 0.14, 0.15, 0.47, 0.45, 0.46, 0.67, 0.48, 0.28, 0.2, 0, 0.55, 0.16), axis_Z = c(0.99, 0.99, 0.99, 0.99, 0.99, 1, 0.98, 0.99, 1, 0.98, 0.85, 0.53, 0.89, 0.83, 0.91, 0.91, 0.84, 0.9, 0.64, 0.8, 0.61, 0.5, 0.59, 0.48, -0.39, 0.74, 0.74, 0.77, 0.77, 0.69)), row.names = c(NA, 30L), class = "data.frame") # calculate end points of cylinders # (cylinders have starting coordinates, a length and a direction as unit vector) cylinder$end_X = cylinder$start_X + cylinder$axis_X * cylinder$length cylinder$end_Y = cylinder$start_Y + cylinder$axis_Y * cylinder$length cylinder$end_Z = cylinder$start_Z + cylinder$axis_Z * cylinder$length # prepare cylinders cylinder_list <- lapply(1:nrow(cylinder), function(i) { cyl <- cylinder3d( center = cbind( c(cylinder$start_X[i], cylinder$end_X[i]), c(cylinder$start_Y[i], cylinder$end_Y[i]), c(cylinder$start_Z[i], cylinder$end_Z[i])), radius = cylinder$radius[i], closed = -2) cyl }) # plot cylinders open3d() par3d(windowRect = c(50,50,650, 650)) shade3d(shapelist3d(cylinder_list, plot = FALSE), color = "blue") I would like the light source to be a point source from infinite distance, as I would like to simulate sunlight. As the sun is so far away, I would just assume the light beams to be parallel to each other.
I believe this does what you want: library(rgl) # some cylinders # ... deleted ... # cylinder$start_Z <- cylinder$start_Z - 361.5 # calculate end points of cylinders # (cylinders have starting coordinates, a length and a direction as unit vector) cylinder$end_X = cylinder$start_X + cylinder$axis_X * cylinder$length cylinder$end_Y = cylinder$start_Y + cylinder$axis_Y * cylinder$length cylinder$end_Z = cylinder$start_Z + cylinder$axis_Z * cylinder$length # prepare cylinders cylinder_list <- lapply(1:nrow(cylinder), function(i) { cyl <- cylinder3d( center = cbind( c(cylinder$start_X[i], cylinder$end_X[i]), c(cylinder$start_Y[i], cylinder$end_Y[i]), c(cylinder$start_Z[i], cylinder$end_Z[i])), radius = cylinder$radius[i], closed = -2) cyl }) cylinder_list <- shapelist3d(cylinder_list, plot = FALSE) # plot cylinders open3d() #> glX #> 1 par3d(windowRect = c(50,50,650, 650)) # shade3d(cylinder_list, color = "blue") # Suppose Sun is in direction xyz sun <- c(0,0,1) # straight up # Get a matrix that projects sun to c(0,0,0), # and leaves anything with z = 0 alone M <- rbind( cbind( diag(2), c(-sun[1]/sun[3], -sun[2]/sun[3])), 0) # Replot the shadows of the cylinders shadows <- transform3d(cylinder_list, t(M)) shade3d(shadows, col = "gray", lit = FALSE) # decorate3d() # Add axes par3d(userMatrix = diag(4)) # Display flat. lowlevel() # And show it in reprex # Use snapshot3d() to get PNG file containing the final image Created on 2022-07-12 by the reprex package (v2.0.1) It should include your definition of the cylinders dataframe, but I left that out to make it easier to read. If you want to show the cylinders as well as the shadows, uncomment the shade3d() call on cylinder_list. You'll probably also want to make the Z values smaller; I subtracted 361.5 to put them near zero. If you don't do this, the graph will be extremely long and thin, and you won't be able to see anything. The idea of the code is to project the sun vector to c(0,0,0), while leaving anything with Z==0 alone. This flattens the cylinders into objects that fit in the Z == 0 plane. Setting the userMatrix to the identity matrix then displays the shadow. Use the snapshot3d() function to make this into a raster image (in a PNG file).
Create a multi-facets plot with common shaded rectangle (geom_rect)
I do apologise in advance but I'm not an R expert (at all). I used R rarely and the last time, I had to convert my data as tibble in order to plot them correctly, and now I did the same but I believe it would be better to convert them into POSIXct (but I don't know how to do it :confused: ) I have this csv: structure(list(Date = c("01/02/2003 01:01:01", "01/03/2003 01:01:01", "01/04/2003 01:01:01", "01/05/2003 01:01:01", "01/06/2003 01:01:01", "01/07/2003 01:01:01", "01/08/2003 01:01:01", "01/09/2003 01:01:01", "01/10/2003 01:01:01", "01/11/2003 01:01:01", "01/12/2003 01:01:01", "01/01/2004 01:01:01", "01/02/2004 01:01:01", "01/03/2004 01:01:01", "01/04/2004 01:01:01", "01/05/2004 01:01:01", "01/06/2004 01:01:01", "01/07/2004 01:01:01", "01/08/2004 01:01:01", "01/09/2004 01:01:01", "01/10/2004 01:01:01", "01/11/2004 01:01:01", "01/12/2004 01:01:01", "01/01/2005 01:01:01", "01/02/2005 01:01:01", "01/03/2005 01:01:01", "01/04/2005 01:01:01", "01/05/2005 01:01:01", "01/06/2005 01:01:01", "01/07/2005 01:01:01", "01/08/2005 01:01:01", "01/09/2005 01:01:01", "01/10/2005 01:01:01", "01/11/2005 01:01:01", "01/12/2005 01:01:01", "01/01/2006 01:01:01", "01/02/2006 01:01:01", "01/03/2006 01:01:01", "01/04/2006 01:01:01", "01/05/2006 01:01:01", "01/06/2006 01:01:01", "01/07/2006 01:01:01", "01/08/2006 01:01:01", "01/09/2006 01:01:01", "01/10/2006 01:01:01", "01/11/2006 01:01:01"), Pz1 = c(0.53, 0.25, 0.3, 0.51, 0.23, 0.52, 0.48, 0.36, 0.56, 0.27, 0.44, 0.28, 0.79, 0.15, 0.73, 0.44, 0.5, 0.26, 0.1, 0.26, 0.69, 0.38, 0.51, 0.39, 0.42, 0.29, 0.68, 0.62, 0.5, 0.06, 0.29, 0.13, 0.6, 0.21, 0.34, 0.17, 0.39, 0.21, 0.89, 0.19, 0.44, 0.53, 0.55, 0.89, 0.55, 0.65), Pz2 = c(0.62, 0.99, 0.87, 0.77, 0.51, 0.66, 0.4, 0.68, 0.87, 0.13, 0.81, 0.29, 0.11, 0.11, 0.23, 0.71, 0.85, 0.05, 0.78, 0.32, 0.16, 0.54, 0.65, 0.09, 0.97, 0.81, 0.49, 0.36, 0.37, 0.78, 0.04, 0.67, 0.91, 0.12, 0.34, 0.3, 0.71, 0.04, 0.73, 0.33, 0.59, 0.23, 0.82, 0.04, 0.04, 0.82), Pz3 = c(0.2, 0.76, 0.04, 0.39, 0.58, 0.49, 0.44, 0.12, 0.16, 0.12, 0.95, 0.95, 0.08, 0.68, 0.57, 0.49, 0.58, 0.46, 0.39, 0.51, 0.69, 0.09, 0.68, 0.18, 0.3, 0.75, 0.76, 0.85, 0.17, 0.6, 0.45, 0.26, 0.65, 0.07, 0.7, 0.71, 0.47, 0.79, 0.58, 0.08, 0.37, 0.86, 0.23, 0.31, 0.06, 0.1), Pz4 = c(0.21, 0.65, 0.67, 0.45, 0.32, 0.79, 0.94, 0.78, 0.73, 0.83, 0.79, 0.46, 0.07, 0.84, 0.25, 0.27, 0.77, 0.37, 0.16, 0.67, 0.88, 0.67, 0.87, 0.95, 0.63, 0.61, 0.21, 0.21, 0.4, 0.74, 0.62, 0.22, 0.08, 0.67, 0.2, 0.18, 0.83, 0.3, 0.15, 0.7, 0.5, 0.43, 0.81, 0.17, 0.31, 0.66), Pz5 = c(0.57, 0.78, 1, 0.87, 0.88, 0.5, 0.24, 0.71, 0.11, 0.4, 0.08, 0.2, 0.67, 0.41, 0.28, 0.45, 0.6, 0.18, 0.27, 0.02, 0.96, 0.48, 0.95, 0.01, 0.8, 0.07, 0.34, 0.09, 0.19, 0.59, 0.34, 0.66, 0.48, 0.86, 0.97, 0.76, 0.93, 0.21, 0.5, 0.93, 0.41, 0.33, 0.32, 0.12, 0.42, 0.94), Pz6 = c(0.42, 0.34, 0.34, 0.73, 0.7, 0.67, 0.09, 0.45, 0.55, 0.88, 0.05, 0.15, 0.85, 0.02, 0.42, 0.14, 0.68, 0.71, 0.57, 0.14, 0.85, 0.81, 0.2, 0.97, 0.42, 0.59, 0.23, 0.39, 0.5, 0.87, 0.37, 0.63, 0.7, 0.3, 0.33, 0.29, 0.9, 0.75, 0.38, 0.17, 0.87, 0.45, 0.79, 0.74, 0.21, 0.05)), class = "data.frame", row.names = c(NA, -46L)) Now... So far I managed to get them into the plot style I need, using the following code: a=read.csv("C:/Users/simon/Desktop/4.csv") b=a c=b %>% mutate(Date = dmy_hms(Date)) %>% arrange(Date) %>% as_tbl_time(index = Date) df=c df_melt = melt(df, id= "Date") d=ggplot(df_melt, aes(x = Date, y = value)) + geom_line() + facet_wrap(~ variable, scales = 'free_y', ncol = 2) d+theme_bw() Which gives this: I know, it's absolutely horrible, but it was the only way I remember. What I do really need is to add a shaded rectangle in every plot (at the same date); something like this, for instance: Could you kindly help me with this matter, please? I don't mind changing the data format to POSIXct (if required for ggplot), anything really, as long as it looks like this. Thanks a lot! P.S. I don't need the data with the time (i.e., 01:01:01 can be removed if needed!)
Add the line geom_rect(aes(xmin=as.POSIXct("2006/01/01"), xmax=as.POSIXct("2006/06/01"), ymin=0, ymax=1), alpha=.01) to your ggplot code.
R: categorize all numeric variables (1:0) according to a cut-off
I have the following data frame: structure(list(test1 = c(0.12, 0.2, 0.55, 0.22, 0.19, 0.17, 0.15, 0.12, 0.32, 0.23, 0.32, 0.23), test2 = c(0.15, 0.12, 0.32, 0.23, 0.12, 0.2, 0.55, 0.22, 0.12, 0.2, 0.55, 0.22), test3 = c(0.07, 0.01, 0, 0.13, 0.16, 0.78, 0.98, 0.1, 0.5, 0.3, 0.4, 0.5), test4 = c(0.23, 0.12, 0.2, 0.2, 0.55, 0.22, 0.12, 0.2, 0.55, 0.22, 0.55, 0.42 )), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame" )) And I am trying to write a script which, for each variables (test1, test2, test3...), creates (and add to the data frame) a dicotomic variable (named as out_testX) depending if the variable value is major or equal to .20. The results, should be something like this: structure(list(test1 = c(0.12, 0.2, 0.55, 0.22, 0.19, 0.17, 0.15, 0.12, 0.32, 0.23, 0.32, 0.23), test2 = c(0.15, 0.12, 0.32, 0.23, 0.12, 0.2, 0.55, 0.22, 0.12, 0.2, 0.55, 0.22), test3 = c(0.07, 0.01, 0, 0.13, 0.16, 0.78, 0.98, 0.1, 0.5, 0.3, 0.4, 0.5), test4 = c(0.23, 0.12, 0.2, 0.2, 0.55, 0.22, 0.12, 0.2, 0.55, 0.22, 0.55, 0.42 ), out_test1 = c(0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1), out_test2 = c(0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1), out_test3 = c(0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1), out_test4 = c(1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1)), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame")) Can anyone help me? Thank you
With mutate_all, we pass the function in the list specify a suffix to be added to column name, and if it needs to be prefix, do this in rename_at library(dplyr) library(stringr) df1 %>% mutate_all( list(out = ~+( . >= .2))) %>% rename_at(vars(ends_with('out')), ~ str_replace(., '(.*)_(out)', '\\2_\\1')) Or using base R df1[paste0("out_", names(df1))] <- +(df1 >= .2)
ggplot2 putting two plots on one
I have some data of x values called "norm". I want to plot a histogram and then plot the density function of a beta with parameters 3.5 and 3 onto the histogram. The main goal of this is to show that the beta fits the norm data. I also need the scale of the y axis to match for both the density and the histogram. I got a plot at one point but the density function was very low because the histogram counted so the y axis went to 30 in my case and obviously the density line was <1. Here is my code: x <- seq(0,1, len = 115) db_trial = dbeta(x, 3.5, 3.0) ggplot(data = norm)+ geom_line(aes(x,db_trial), col = "red", stat = "density")+ geom_histogram(aes(y = ...density...), bins = 10, alpha = .2, fill = "green", col = "red") Here is my data set norm which is just the presidents data set in R but divided by 100. # dput(norm) structure(list(approval_rate = c(0.87, 0.82, 0.75, 0.63, 0.5, 0.43, 0.32, 0.35, 0.6, 0.54, 0.55, 0.36, 0.39, 0.69, 0.57, 0.57, 0.51, 0.45, 0.37, 0.46, 0.39, 0.36, 0.24, 0.32, 0.23, 0.25, 0.32, 0.32, 0.59, 0.74, 0.75, 0.6, 0.71, 0.61, 0.71, 0.57, 0.71, 0.68, 0.79, 0.73, 0.76, 0.71, 0.67, 0.75, 0.79, 0.62, 0.63, 0.57, 0.6, 0.49, 0.48, 0.52, 0.57, 0.62, 0.61, 0.66, 0.71, 0.62, 0.61, 0.57, 0.72, 0.83, 0.71, 0.78, 0.79, 0.71, 0.62, 0.74, 0.76, 0.64, 0.62, 0.57, 0.8, 0.73, 0.69, 0.69, 0.71, 0.64, 0.69, 0.62, 0.63, 0.46, 0.56, 0.44, 0.44, 0.52, 0.38, 0.46, 0.36, 0.49, 0.35, 0.44, 0.59, 0.65, 0.65, 0.56, 0.66, 0.53, 0.61, 0.52, 0.51, 0.48, 0.54, 0.49, 0.49, 0.61, 0.68, 0.44, 0.4, 0.27, 0.28, 0.25, 0.24, 0.24, 0.01 )), .Names = "approval_rate", row.names = c(NA, -115L), class = "data.frame") This returns an error "Stat_bin requires the following missing aesthetics: x". What am I doing wrong. I am a novice with ggplot2.
It's usually better to use stat_function for this type of thing. Note that I'm technically using an anonymous function that wraps dbeta, so you can adjust the height of the curve via multiplication. g <- ggplot(data = norm, aes(x = approval_rate))+ geom_histogram() + stat_function(fun = function(x) dbeta(x, shape1 = 3.5, shape2 = 3.0) * 5, color = 'red') print(g)
Error with hist function (need finite 'ylim' values) (breaks related)
Could anyone tell why running hist on matrix CHh gives error "Error in plot.window(xlim, ylim, "", ...) : need finite 'ylim' values"? If I eliminate the min function the error disappears. Yet I don't understand why that represents a problem. Thank you. CFh <-structure(c(-0.64, 0.34, 0.65, 0.26, -0.64, 0.92, -0.64, -0.1, -0.41, -0.36, 0.16, 0.92, 1.43, -0.41, 0.65, 0.28, 0.47, 0.35, -0.54, 0.65, 0.28, -0.1, 0.92, -0.36, 0.25, 0.34, -0.34, 0.07, 0.65, 0, -0.04, 0.47, 0.78, 0.47, 1.43, -0.23, -0.41, 0.28, 0.62, 0.35, -0.34, -0.23, -0.36, 0.28, 0.26, 0.03, 0.28, 0.07, 0.47, 0.63, 0.35, 0.47, 0, -0.28, 0.34, 0.16, 0.62, -0.04, 0.03, -0.41, -0.34, -0.64, -0.32, -0.28, -0.04, -0.36, 0.34, 0.47, 0.63, 0.62, 0, -0.04, -0.23, 0.65, -0.04, 0.47, -0.64, 0, -0.34, 0.28, -0.1, -0.28, 0.35, -0.34, -0.04, 0.63, 0.92, 0.35, 0.25, 0.34, 0.25, 0.34, 0.16, -0.36, 0, 0.28, 0.28, -0.28, -0.34, -0.23, 0.78, -0.41, 0.65, -0.32, -0.54, -0.36, 0.92, 0.25, 0.47, -0.1, 0.78, -0.54, 0.63, 0.65, -0.28, 0.25, 0.07, 0.35, 0.62, -0.28, -0.36, -0.54, 0.47, 0.47, 1.43, 0.63, -0.28, 0.03, 0.92, 0.92), .Dim = c(26L, 5L)) Breaks <- c(max(CFh,1.0), 1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0, -0.1, -0.2, -0.3, -0.4, -0.5, -0.6, -0.7, -0.8, -0.9, -1.0, min(CFh,-1.0)) h <- hist(CFh, plot=TRUE, breaks=Breaks)
You have repeated values in your Breaks vector. This causes a problem with binning. Make sure the values are unique h <- hist(CFh, plot=TRUE, breaks=unique(Breaks))
Continuing on from the above solution, using unique breaks, you could also consider a simpler way to create breaks as a sequence Breaks <- unique(c(max(CFh,1.0), seq(1, -1, by=-0.1), min(CFh,-1.0))) h <- hist(CFh, plot=TRUE, breaks=Breaks)