Reputation: 685
I have a heat map that turns red for any values outside of a specified range and has a gradient fill for values within that range, which is a question I posted earlier and got a solution to here. I am trying to apply this same gradient fill to a histogram of those values. Similar to what this post does with a rainbow fill, except I want my fill to align with the values as dictated by the same fill in the heat map. My adaptation to the histogram produces a legend with the correct fill, but the fill is still grey. I realize that the bins may need to be adjusted to accommodate this request as the fill cuttoffs have the possibility of being in the middle of a bin. Sample code of my attempt is below.
#Check packages to use in library
{
library('shiny') #allows for the shiny app to be used
library('ggplot2')
library('dplyr')
library('stringr') #string opperator
library('scales')
}
#Data
horizontal_position <- c(-2, -1, 0, 1, 2)
vertical_position <- c(-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2)
data_val <- sample(-25:100, 25)
all_data <-data.frame(horizontal_position, vertical_position, data_val)
# UI
ui <- fluidPage(
fluidRow(
column(6,
wellPanel(
plotOutput("plot1")
)),
column(4,
wellPanel(
plotOutput("plot2"))
)
)
)
#SERVER
server <- function(input, output, session)
{
output$plot1 <- renderPlot({
all_data %>%
mutate(DATA = replace(data_val, data_val > 75, NA)) %>%
ggplot(aes(horizontal_position, vertical_position)) +
geom_tile(aes(fill = DATA), colour = "black") +
geom_text(aes(label = data_val),colour="white", size = 10)+
scale_fill_gradientn(colours = c("blue4", "blue", "dodgerblue", "turquoise1"),
breaks=c(0, 25, 50, 75, Inf), limits = c(0,75),
na.value = "red") +
labs(x="Horizontal Position", y="Vertical Position") +
theme(plot.title = element_text(hjust = 0.5, size=20))
})
output$plot2 <- renderPlot({
all_data %>%
mutate(DATA = replace(data_val, data_val > 75, NA)) %>%
ggplot(aes(all_data$data_val)) +
geom_histogram(binwidth = 5, boundary = min(all_data$data_val),
aes(fill = DATA), colour = "black") +
scale_x_continuous(breaks = seq(min(all_data$data_val), max(all_data$data_val) + 4, by =5)) +
scale_fill_gradientn(colours = c("blue4", "blue", "dodgerblue", "turquoise1"),
breaks=c(0, 25, 50, 75, Inf), limits = c(0,75),
na.value = "red") +
labs(x="Data Value", y="Count", title = "Histogram of Values") +
theme(plot.title = element_text(hjust = 0.5, size=20))
})
}
#Run the Shiny App to Display Webpage
shinyApp(ui=ui, server=server)
Upvotes: 2
Views: 543
Reputation: 54247
You could do:
library(ggplot2)
all_data <- structure(list(horizontal_position = c(-2, -1, 0, 1, 2, -2, -1,
0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2),
vertical_position = c(-2, -2, -2, -2, -2, -1, -1, -1, -1,
-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2), data_val = c(-11L,
20L, 86L, 6L, 53L, 95L, 21L, 92L, 8L, 88L, 74L, 25L, 9L,
51L, 94L, -16L, -10L, 83L, 62L, -19L, 0L, 23L, 76L, 14L,
79L)), .Names = c("horizontal_position", "vertical_position",
"data_val"), row.names = c(NA, -25L), class = "data.frame")
ggplot(all_data, aes(data_val)) +
geom_histogram(aes(fill = ..x..), binwidth = 5) +
scale_fill_gradientn(
colours = c("blue4", "blue", "dodgerblue", "turquoise1"),
breaks=c(0, 25, 50, 75, Inf), limits = c(0,75),
na.value = "red"
)
Upvotes: 4