ggplot2 putting two plots on one - r

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)

Related

Radar plot in R Shiny

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.

How to fill the area under/above the curve with different colors above and below 0?

I would like to plot a time series of the Standardized Precipitation Index (SPI). Normally this looks somewhat like this:
You can see that the area under/above the curve is colored in blue/red. This is what I would like to plot too.
I know that there were kind of similar questions, like this and that one. This could bring be a bit further, but unfortunately not to the final result yet.
To make it easier to understand, here is some code so that everybody can reproduce it:
library(data.table)
library(ggplot2)
vec1 <- 1:310
vec2 <- c(1.78, 1.88, 1.10, 0.42, 0.73, 1.35, 1.34, 0.54, 0.20, 0.72, 1.29, 1.78, 1.30, 1.37, -0.13, 0.64, -0.13, 0.87, 0.47, -0.26, -0.27,
-0.81, -0.54, -0.77, -0.29, -0.22, -0.05, 0.41, 0.45, 0.91, -0.31, 0.67, 0.28, 0.93, 0.43, -0.04, -0.80, -1.20, -0.73, -0.98, 0.47, -0.01,
1.30, 1.45, 0.72, -0.59, -1.14, -0.33, 0.22, 0.49, 0.58, 0.36, 0.66, 0.64, 0.47, -0.60, 1.01, 1.50, 1.18, 0.82, 0.02, 0.57, 0.25,
1.20, 1.19, 0.71, -0.30, -1.37, -1.50, -1.03, -0.77, -1.08, -1.92, -2.32, -2.46, -1.61, -0.39, 0.67, 0.38, 0.62, -0.34, 0.01, -0.55, -0.74,
-1.95, -1.18, -0.96, 0.36, -0.96, -1.28, -2.29, -2.67, -0.65, -0.13, 0.61, 0.21, 0.57, 0.11, 0.37, 0.20, -0.14, -0.87, -0.84, 0.87, 1.33,
0.45, -0.76, -1.27, -0.65, -0.29, 0.54, 0.14, -0.55, -0.94, -0.98, -0.44, -0.37, 0.72, 0.70, 0.95, 0.89, 1.10, 1.51, 1.11, 1.77, 1.20,
1.23, -0.72, -1.43, -2.11, -1.37, -0.80, -0.34, -0.14, 0.22, -0.65, -0.44, -0.86, -0.46, -0.67, -0.91, -0.40, -0.09, 0.22, 0.96, 0.71, 0.51,
-1.61, -1.62, -1.43, -0.27, 1.08, 1.76, 1.30, 0.78, 1.02, 1.01, 0.56, -0.32, 0.37, 0.31, 1.36, 1.49, 1.42, 0.78, -0.19, 0.64, 0.39,
0.47, -1.13, -1.45, -0.52, 0.43, -0.19, -0.97, -0.27, 0.63, 1.01, 1.01, 0.83, -0.56, -1.71, -0.29, 1.06, 1.82, 1.28, 0.88, 1.08, 1.78,
1.47, 0.74, -0.34, 0.14, 1.09, 1.49, 1.30, 0.28, -0.25, 0.24, -0.33, 0.05, -0.86, -0.69, -1.03, -0.59, 0.32, 0.61, 0.84, -0.18, -0.67,
0.46, 0.31, -0.72, -2.26, -2.85, -0.69, -0.77, 0.64, -1.49, -1.69, -1.55, -0.28, -0.80, -1.15, -0.38, 0.31, 0.18, -0.27, -0.84, -0.94, -1.23,
-0.53, -1.52, -0.73, -0.93, 0.25, -0.11, 0.38, 0.48, 0.10, -0.02, 0.26, 1.39, 1.61, 0.83, 0.09, 0.95, 1.07, 0.77, 0.23, 0.26, 0.85,
0.93, 0.91, 1.10, 0.47, 0.74, 1.42, 1.17, 0.32, -0.40, 0.76, 1.44, 1.69, 1.03, 0.01, 0.46, 0.61, 0.60, -0.09, -0.31, -0.96, -0.91,
-0.06, 0.75, 1.32, 1.29, 0.55, 0.43, -1.25, 0.12, -0.05, 0.18, -0.77, -2.19, -1.85, -2.12, -1.51, -1.14, -0.79, -0.82, -1.13, -1.72, -2.14,
-1.95, -0.63, 0.70, 0.64, 0.17, -1.04, -0.58, -0.57, -0.57, -1.05, -1.11, -0.59, -0.07, 1.22, 0.30, -0.15)
df <- data.frame(vec1, vec2)
colnames(df) <- c("ID", "SPI")
df = as.data.table(df)
There you have the entire SPI time series, one value for each month between 1980 and 2005.
So far I came to that result:
ggplot(data = df, aes(x = ID, y = SPI)) +
geom_col(data = df[SPI <= 0], fill = "red") +
geom_col(data = df[SPI >= 0], fill = "blue") +
theme_bw()
When you run the code you will see the following plot:
This is going into the right direction, but the small white gaps between values in the first half are disturbing and not supposed to be there. So there must be something wrong.
It is supposed to look like this, just with the two colors blue and red for the positive and negative values:
ggplot(data = df, aes(x = ID, y = SPI)) +
geom_area() +
theme_bw()
The code leads to this image:
You can see that there are no white gaps between the single values and I have absolutely no idea what leads to these errors.
Anybody with an idea how to solve that?
OP, what you are observing are the artifacts related to the resolution of your graphics driver and the space between columns. The areas you show are composed of many filled columns next to one another on the x axis. You do not specify the width= argument for geom_col(), so the default value leaves a space between the individual values on the x axis. It's best to illustrate if we take only a section of your data along the x axis:
ggplot(data = df, aes(x = ID, y = SPI)) +
geom_col(data = df[SPI <= 0], fill = "red") +
geom_col(data = df[SPI >= 0], fill = "blue") +
theme_bw() +
xlim(0,100) # just the first part on the left
There's your white lines - it's the space bewtween the columns. When you have the larger picture, the appearance of the white lines has to do with the resolution of your graphics device. You can test this if you save your graphic with ggsave() using different parameters for dpi=. For example, on my computer saving ggsave('filename.png', dpi=72) gives no lines, but ggsave('filename.png', dpi=600) shows the white lines in places.
There's an easy solution to this though, which is to specify the width= argument of geom_col() to be 1. Be default, it's set to 0.75 or 0.8 (not exactly sure), which leaves a gap between the next value (fills ~75 or 80% of the space). If you set this to 1, it fills 100% of the space allotted for that column, leaving no white space in-between:
ggplot(data = df, aes(x = ID, y = SPI)) +
geom_col(data = df[SPI <= 0], fill = "red", width=1) +
geom_col(data = df[SPI >= 0], fill = "blue", width=1) +
theme_bw() +
xlim(0,100)
What if you change the distance between columns?
Just added "width=1" inside "aes" of ggplot.
ggplot(data = df, aes(x = ID, y = SPI, width=1)) +
geom_col(data = df[SPI <= 0], fill = "red") +
geom_col(data = df[SPI >= 0], fill = "blue") +
theme_bw()
I get this:
image.result

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.

How to keep order of the correlation plot labels as same in the datafile?

In Correlation plot, How can I keep the labels in the same order as in datafile, without rearranging?
This is the scripts I have been using;
library(corrplot)
library(RColorBrewer)
library(Hmisc)
CORR <- df
M <- cor(CORE)
pM <- rcorr(M)
corrplot(pM$r,
type="upper",
order="hclust",
tl.cex = 0.5,
col=colorRampPalette(c("blue4", "white", "firebrick1"))(100),
p.mat = pM$P,
sig.level = 0.05,
insig = "blank"
)
sample data
df <- structure(list(Cytosol_1 = c(2.8, 0.31, 1.21, 1.84, 0.93, 2.71, 2.03, 0.93, 0.89, 0.4, 0.32, 1.8, 1.16, 0.39, 1.16, 0.76, 1.17, 0.95, 0.58, 2.68, 0.88, 0.59, 1.49, 0.48, 0.51, 1.04, 0.89, 3.48, 1.47), Cytosol_2 = c(1.61, 0.22, 1.42, 1.97, 0.88, 1.46, 1.43, 0.74, 0.72, 0.43, 0.51, 2.07, 1.29, 0.71, 0.92, 0.57, 1.9, 0.84, 0.4, 2.72, 1.08, 0.96, 1.75, 0.24, 0.76, 0.99, 2.35, 2.06, 1.24 ), Cytosol_3 = c(1.76, 0.27, 0.77, 1.23, 0.93, 1.43, 0.7, 0.44, 0.58, 0.47, 0.57, 0.85, 0.79, 0.75, 0.95, 0.85, 1.49, 1.19, 0.72, 1.92, 1.11, 1.18, 1.03, 0.75, 0.58, 0.7, 0.79, 1.64, 1.14), Cytosol_4 = c(1.41, 0.98, 0.73, 2.31, 1.07, 1.1, 1.31, 0.66, 0.69, 0.51, 0.53, 1.3, 1.37, 1.55, 0.99, 1.27, 1.22, 0.89, 1.21, 1.56, 1.14, 0.7, 0.3, 0.63, 1.73, 1.49, 0.92, 1.8, 1.7), Cytosol_5 = c(2.36, 0.25, 1.43, 2.76, 0.91, 2.88, 2.73, 0.79, 0.71, 0.15, 0.92, 1.94, 1.12, 0.64, 2.07, 0.68, 1.51, 0.66, 0.51, 1.91, 1.61, 0.68, 0.73, 0.7, 0.94, 1.24, 2.45, 3.12, 1.58), Ribosome_6 = c(1.52, 1.09, 1.58, 1.29, 0.92, 1.12, 0.8, 0.51, 0.87, 0.58, 0.42, 0.72, 1.39, 1.14, 1.87, 1.11, 1.11, 1.07, 0.84, 1.11, 1.17, 0.45, 0.49, 0.59, 0.89, 0.79, 0.67, 1.66, 1.86), Ribosome_7 = c(4.16, 0.98, 1.79, 2.21, 1.21, 1.31, 1.01, 0.02, 0.82, 0.51, 0.81, 0.73, 2.34, 1.04, 1.99, 0.92, 2.2, 0.74, 0.25, 1.71, 1.43, 0.67, 1.19, 0.49, 1.5, 1.14, 0.92, 3.67, 2.68), Ribosome_8 = c(2.02, 0.95, 1.79, 1.47, 0.87, 1.88, 0.97, 0.51, 0.77, 0.66, 0.54, 1, 1.54, 0.92, 1.73, 1.32, 1.89, 0.97, 0.87, 1.26, 1.1, 0.61, 0.49, 0.57, 0.91, 0.76, 0.86, 3.37, 3.1), Ribosome_9 = c(2.67, 0.45, 1.45, 1.41, 0.56, 1.93, 1.29, 0.44, 0.58, 0.38, 0.3, 1.15, 1.5, 0.67, 1.38, 0.72, 1.71, 0.74, 0.5, 2.2, 1.36, 0.74, 1.06, 0.54, 0.72, 0.83, 1.27, 2.55, 1.4), Ribosome_10 = c(2.19, 0.55, 1.39, 1.56, 0.62, 1.67, 1.31, 0.47, 0.46, 0.35, 0.4, 1.02, 1.32, 0.7, 0.96, 0.6, 1.63, 0.94, 0.38, 1.6, 0.92, 0.71, 0.81, 0.56, 0.77, 0.73, 1.14, 2.42, 1.11 ), ER_17 = c(1.41, 0.29, 0.32, 0.76, 0.7, 2.75, 2.78, 0.8, 0.96, 0.28, 0.82, 2.15, 1.19, 0.55, 0.78, 0.97, 1.42, 1.22, 0.92, 1.84, 0.6, 0.69, 0.43, 0.39, 0.38, 0.48, 0.36, 2.17, 1.03), ER_18 = c(1.02, 0.43, 0.98, 1.91, 0.88, 3.19, 1.05, 1.65, 0.53, 1.08, 0.39, 1.1, 0.36, 0.56, 0.58, 1.13, 1.25, 1.03, 0.79, 1.67, 0.56, 0.84, 1.17, 1.05, 0.18, 0.69, 0.09, 1.58, 0.47), ER_19 = c(0.58, 0.72, 0.58, 1.42, 0.52, 2.18, 0.95, 0.32, 1.44, 0.86, 0.24, 0.8, 0.62, 0.34, 0, 1.29, 1.29, 0.84, 1.43, 4.48, 1.82, 0.97, 0.83, 1.25, 0.29, 0.03, 1.48, 1.72, 1.89), Extracellular_20 = c(2.77, 0.43, 2.23, 1.86, 0.51, 2.96, 1.41, 0.91, 0.61, 0.64, 0.84, 2.03, 1.34, 0.69, 0.82, 0.5, 1.18, 0.77, 0.59, 1.65, 1, 0.74, 1, 0.55, 1.38, 1.38, 1.82, 1.58, 1.02), Extracellular_21 = c(3.4, 0.67, 1.91, 1.76, 0.77, 1.65, 1.04, 0.48, 0.53, 0.34, 0.48, 1.24, 1.49, 1.07, 1.24, 0.81, 1.4, 0.85, 0.46, 1.52, 0.9, 0.81, 0.6, 0.57, 1.32, 1.3, 2.22, 1.29, 0.98), Extracellular_22 = c(0.52, 0.01, 0.33, 2.25, 1.05, 2.63, 2.5, 0.99, 0.53, 1.21, 1.43, 3.2, 0.32, 0.2, 0.23, 0.52, 1.07, 0.51, 0.55, 2.58, 1.5, 1.19, 2.16, 0.81, 0.02, 0.1, 0.2, 2.38, 1.36), Extracellular_23 = c(0.16, 0, 0, 1.8, 0.74, 1.45, 2.6, 0.8, 1.68, 1.63, 2.41, 1.46, 0.52, 0.67, 0.05, 1.12, 0.78, 0.62, 0.56, 1.74, 1.03, 1.74, 1.14, 1.33, 0, 0, 0, 0.96, 1.46)), class = "data.frame", row.names = c(NA, -29L))
The corrplot function has an "order" argument, which allows you to specify how the rows and columns of the plot are arranged. Setting order = 'original' preserves the ordering in the source data frame:
corrplot(pM$r,
type="upper",
order="original",
tl.cex = 0.5,
col=colorRampPalette(c("blue4", "white", "firebrick1"))(100),
p.mat = pM$P,
sig.level = 0.05,
insig = "blank"
)

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)

Resources