ggplotly() ignores legend and produce different plot with ggplot legend - r

I try to use ggplotly to run ggplot graph, but the legend label not showing the same things. Why it is? Please help. Thanks
And also any idea to ignore the warning of changing to numeric data, so it doesnt show too many warning when run it through shiny.
Thanks a lot
The code are below
structure(list(...1 = c("1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
"31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41",
"42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52",
"53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63",
"64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74",
"75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85",
"86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96",
"97", "98", "99", "100", "101", "102", "103", "104", "105", "106",
"107", "108", "109", "110", "111", "112", "113", "114", "115",
"116", "117", "118", "119", "120", "121", "122", "123", "124",
"125", "126", "127", "128", "129", "130", "131", "132", "133",
"134", "135", "136", "137", "138", "139", "140", "141", "142",
"143", "144", "145", "146", "147", "148", "149"), indexlist = c(1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149), datainput = c("112069",
"7377.02", "Unanswered", "675900", "Unanswered", "17323000",
"1935328.98", "411079", "Unanswered", "38530.29", "96.5", "89268",
"6380000", "32185.99", "102103", "Unanswered", "Question no match",
"Unanswered", "Unanswered", "1441914.2080000001", "681325", "89340.307000000001",
"234", "9278", "9809", "259550", "675900", "Unanswered", "168322",
"Unanswered", "435708.78", "962.15899999999999", "681325", "81000",
"38759", "Unanswered", "Question no match", "Unanswered", "195747",
"Unanswered", "7070890", "10739506", "65430.91", "Unanswered",
"61900", "Unanswered", "Unanswered", "5130068", "11556", "Unanswered",
"Unanswered", "102364", "Unanswered", "103451.19", "9756559.5299999993",
"16520", "644039", "16.187999999999999", "Unanswered", "Unanswered",
"13154.44", "Question no match", "Question no match", "125131",
"Unanswered", "Unanswered", "Unanswered", "608470.29", "Question no match",
"Unanswered", "Unanswered", "Unanswered", "10496.82", "195747",
"21399", "Unanswered", "214050", "1439.18", "681104", "10587765",
"11816", "69528", "Unanswered", "26519409", "Question no match",
"1013315", "17323000", "114016", "117723", "Unanswered", "Question no match",
"555872.6", "8442.34", "1995000", "Unanswered", "7208", "152495",
"372366", "132191.5", "21399", "Unanswered", "195747", "3207.89",
"Unanswered", "77629", "195747", "Question no match", "Unanswered",
"400", "Unanswered", "555872.6", "3291303", "110296.5", "Unanswered",
"55715.991999999998", "186011", "Unanswered", "Question no match",
"Unanswered", "385000", "Unanswered", "142829.75599999999", "125131",
"Question no match", "20981", "Unanswered", "186011", "9701.8629999999994",
"Unanswered", "102103", "5138", "4395555.97", "118398.916", "1638.58",
"2749023", "Unanswered", "9394598", "20960", "17323000", "1232.19",
"240468", "6963.1", "Unanswered", "348.99400000000003", "2513000",
"4449880.6100000003", "Unanswered", "Unanswered", "27522854"),
verification = c("Yes", "no information", "no answer", "Yes",
"no answer", "Yes", "Yes", "Yes", "no information", "no information",
"no information", "no information", "Yes", "Yes", "Yes",
"no answer", "No", "no information", "no answer", "Yes",
"Yes", "no information", "no information", "Yes", "Yes",
"Yes", "Yes", "no answer", "No", "no answer", "no information",
"no information", "Yes", "no information", "Yes", "no answer",
"No", "no information", "Yes", "no answer", "Yes", "Yes",
"Yes", "no answer", "Yes", "no answer", "no answer", "No",
"No", "no answer", "no information", "Yes", "no answer",
"Yes", "Yes", "Yes", "Yes", "No", "no answer", "no answer",
"Yes", "no information", "No", "No", "no information", "no answer",
"no answer", "No", "no information", "no answer", "no answer",
"no information", "No", "Yes", "No", "no answer", "Yes",
"Yes", "Yes", "Yes", "no information", "Yes", "no answer",
"Yes", "no information", "Yes", "Yes", "Yes", "Yes", "no answer",
"no information", "No", "no information", "Yes", "no answer",
"Yes", "Yes", "Yes", "Yes", "No", "no answer", "Yes", "Yes",
"no answer", "No", "Yes", "no information", "no answer",
"no information", "no answer", "No", "Yes", "No", "no information",
"No", "no answer", "no answer", "no information", "no answer",
"Yes", "no answer", "Yes", "No", "no information", "Yes",
"no answer", "no answer", "No", "no answer", "Yes", "no information",
"Yes", "No", "Yes", "Yes", "no answer", "Yes", "Yes", "Yes",
"Yes", "No", "No", "no answer", "no information", "Yes",
"Yes", "no answer", "no answer", "Yes")), row.names = c(NA,
-149L), class = c("tbl_df", "tbl", "data.frame"))->data_a
p <- data_a%>%
select(indexlist, datainput, verification) %>%
mutate_at(c("datainput"), as.numeric)%>%
drop_na(c("datainput"))%>%
ggplot(aes(x=1:length(`datainput`), y=`datainput`, label= `indexlist`, color = `verification` == "Yes"))+
scale_colour_manual(name = 'Verification',breaks = c("TRUE", "FALSE"), values = c("green", "red"), labels = c("Verified", "Non-Verified"))+
geom_point(size=1.5, alpha = 0.4)+
geom_text(aes(label= ifelse(`datainput` > quantile(`datainput`, 0.975,na.rm = T), `indexlist`,"")), vjust = "inward", hjust = "inward", size = 2, color = "grey50")+
theme_minimal()+
labs(title = "datainput Details",
x = "",
y = "")+
theme(
axis.text.x = element_text(size = 5.5),
axis.text.y = element_text(size = 5.5),
plot.title = element_text(color = "grey40", size = 9, face = "bold"))
ggplotly(p)
I have tried scale_manual_fill and colour but it doesnt work

Try to keep data cleaning/preparation separate from plotting, see cleaned data and plot, now the ggplot and plotly look the same:
library(tidyverse)
library(plotly)
# prepare the data
plotData <- data_a %>%
select(indexlist, datainput, verification) %>%
# remove non-numeric rows before converting
filter(!grepl("^[^0-9.]+$", datainput)) %>%
# prepare data for plotting
mutate(datainput = as.numeric(datainput),
x = seq(n()),
Verification = factor(ifelse(verification == "Yes", "Verified", "Non-Verified"),
levels = c("Verified", "Non-Verified")),
label = ifelse(datainput > quantile(datainput, 0.975, na.rm = TRUE),
indexlist, ""))
# then plot with clean data
p <- ggplot(plotData, aes(x = x, y = datainput,
color = Verification, label = label)) +
scale_colour_manual(values = c("green", "red"))+
geom_point(size = 1.5, alpha = 0.4) +
geom_text(vjust = "inward", hjust = "inward", size = 2, color = "grey50") +
theme_minimal() +
labs(title = "datainput Details", x = "", y = "") +
theme(axis.text.x = element_text(size = 5.5),
axis.text.y = element_text(size = 5.5),
plot.title = element_text(color = "grey40", size = 9, face = "bold"))
# now plotly
ggplotly(p)
ggplot
plotly

I have tried cleaning your data manipulation process by preparing the data before plotting.
library(dplyr)
library(plotly)
library(ggplot2)
p1 <- data_a %>%
filter(grepl('\\d+', datainput)) %>%
mutate(datainput = as.numeric(datainput),
row = as.numeric(`...1`),
verification = ifelse(verification == 'Yes', 'verified', 'Non-Verified')) %>%
ggplot(aes(row, datainput, color = verification)) +
scale_colour_manual(name = 'Verification',
values = c("green", "red")) +
geom_point(size=1.5, alpha = 0.4)+
geom_text(aes(label= ifelse(datainput > quantile(datainput, 0.975,na.rm = TRUE), indexlist,"")),
vjust = -2, hjust = "inward", size = 2, color = "grey50") +
theme_minimal()+
labs(title = "datainput Details",
x = "",
y = "")+
theme(
axis.text.x = element_text(size = 5.5),
axis.text.y = element_text(size = 5.5),
plot.title = element_text(color = "grey40", size = 9, face = "bold"))
plotly::ggplotly(p1)

Related

How to extend DT datatable cells across multiple columns inside table and in header

I'm having extreme difficulty trying to make a character string spread out across multiple columns in a DT:datatable header & in the table itself. I found these solutions:
Extend a table cell across multiple columns,
merge columns in DT:datatable
but can't seem to get them to work for my own purposes.
This is what I am getting:
This is what I want:
Sample Data:
df<-structure(list(`AQS ID` = c(NA, "AQS ID", "340071001", "340170006",
"340010006", "340070002", "340273001", "340150002", "340290006",
"340410007", "340190001", "340030006", "340110007", "340250005",
"340130003", "340315001", "340210005", "340230011", "340219991"
), State = c(NA, "State", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ",
"NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ"
), Site = c(NA, "Site", "Ancora State Hospital", "Bayonne", "Brigantine",
"Camden Spruce St", "Chester", "Clarksboro", "Colliers Mills",
"Columbia", "Flemington", "Leonia", "Millville", "Monmouth University",
"Newark Firehouse", "Ramapo", "Rider University", "Rutgers University",
"Washington Crossing"), `4th Max (ppb)` = c("AQS AMP450 (4-10-19)",
"2015", "72", "77", "64", "79", "70", "76", "75", "66", "73",
"76", "68", "77", "72", "71", "73", "77", "75"), ...5 = c(NA,
2016, 64, 68, 63, 76, 67, 74, 71, 65, 73, 73, 68, 68, 68, 68,
71, 75, 74), ...6 = c(NA, 2017, 68, 67, 63, 76, 70, 73, 74, 64,
72, 74, 63, 60, 64, 66, 69, 75, 71), ...7 = c(NA, 2018, 68, 78,
63, 75, 73, 77, 74, 67, 72, 79, 63, 68, 71, 69, 76, 76, 77),
...8 = c("Envista", "2019", "67", "65", "59", "70", "62",
"68", "68", "58", "66", "71", "68", "67", "65", "64", "66",
"70", "67"), `Design Value 2017` = c("2017", "68", "70",
"63", "77", "69", "74", "73", "65", "72", "74", "66", "68",
"68", "68", "71", "75", "73", NA), `Design Value 2018` = c(2018,
66, 71, 63, 75, 70, 74, 73, 65, 72, 75, 64, 65, 67, 67, 72,
75, 74, NA), `Design Value 2019` = c(2019, 67, 70, 61, 73,
68, 72, 72, 63, 70, 74, 64, 65, 66, 66, 70, 73, 71, NA)), row.names = c(NA,
-19L), class = c("tbl_df", "tbl", "data.frame"))
Sample Code:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(DT::dataTableOutput("dailytable")))
server <- function(input, output) {
jsc <- '
function(settings, json) {
$("td:contains(\'AQS AMP450\')").attr("colspan", "4").css("text-align", "center");
$("tbody > tr:fifth-child > td:empty");
}'
output$dailytable<-renderDataTable({
DT::datatable(df,filter = 'top',options = list(dom = "t", ordering = FALSE, initComplete = JS(jsc)),
class = 'cell-border stripe')
})
}
shinyApp(ui, server)
As you can see, columns are getting pushed over to the right which is not what I want. I would appreciate any help or guidance. Thanks.
Not exactly what you want, but close:
library(htmltools)
sketch <- withTags(
table(
class = "display",
thead(
tr(
th(colspan = 3, "2019", style = "border-right: solid 2px;"),
th(colspan = 5, "4th Max ppb", style = "border-right: solid 2px;"),
th(colspan = 3, "Design Values")
),
tr(
th(colspan = 3, "", style = "border-right: solid 2px;"),
th(colspan = 4, "AQS AMP450 (4-10-19)", style = "border-right: solid 2px;"),
th("Envista", style = "border-right: solid 2px;"),
th(colspan = 3, "")
),
tr(
th("AQS ID"),
th("State"),
th("Site", style = "border-right: solid 2px;"),
th("2015"),
th("2016"),
th("2017"),
th("2018", style = "border-right: solid 2px;"),
th("2019", style = "border-right: solid 2px;"),
th("2017"),
th("2018"),
th("2019")
)
)
)
)
dat <- cbind(df[3:nrow(df),1:8], df[2:(nrow(df)-1), 9:11])
library(DT)
datatable(dat, rownames = FALSE, container = sketch,
options = list(
columnDefs = list(
list(targets = "_all", className = "dt-center")
)
)) %>%
formatStyle(c(3,7,8), `border-right` = "solid 2px")

Deleting Duplicate Data & Replacing missing values in certain areas

So I have been looking at this code which is originally an excel sheet. Once the data set is put into R Studio then I have a few issues.
First of all I changed all the blank cells into NA once I run
CarparkData[is.na(CarparkData)] <- 0
it only changes the data which was originally NA not a blank cell.
Secondly deleting duplicate data, I used the following code and nothing happened.
library("dplyr")
install.packages("tidyverse")
library(tidyverse)
x <-CarparkData
duplicated(x)
x[duplicated(x),]
x[!duplicated(x),]
As I have a row for Date and Time I would like to use this as the column to delete the rows of duplicated data. As I have data which are the same but they are at different times compared to data which is the same and the date and time is the same.
And Thirdly Replacing missing Values
Some of the data has FULL written on it and I would like to home into one column and then change FULL to the number that is full in that specific car park, so changing the FULL cells in that column and not all the FULL cells.
Sample Data
> dput(head(CarparkData))
structure(list(Parnell = c(188L, 183L, 185L, 229L, 237L, 272L
), Ilac = c(665, 683, 694, 769, 786, 839), Jervis = c(421, 408,
403, 417, 423, 455), Arnotts = c(340, 344, 350, 359, 359, 355
), Malboro = c(160L, 160L, 156L, 157L, 173L, 207L), Abbey = c(0,
0, 0, 0, 0, 0), `Thomas Street` = c(173, 173, 173, 186, 189,
198), `Christ Church` = c(77, 76, 74, 73, 83, 91), Setanta = structure(c(24L,
23L, 23L, NA, NA, 46L), .Label = c("10", "100", "101", "102",
"103", "104", "107", "108", "110", "111", "112", "113", "114",
"115", "120", "123", "125", "128", "129", "131", "14", "17",
"19", "21", "24", "27", "28", "29", "30", "31", "32", "34", "36",
"39", "40", "44", "45", "47", "48", "51", "52", "53", "56", "57",
"6", "60", "63", "66", "67", "7", "70", "72", "74", "78", "79",
"80", "81", "82", "84", "85", "86", "89", "9", "91", "92", "93",
"94", "96", "98", "FULL"), class = "factor"), Dawson = c(70,
87, 83, 118, 122, 140), Trinity = c(142L, 143L, 145L, 165L, 167L,
191L), Greenrcs = structure(c(NA, 8L, 9L, NA, 4L, 5L), .Label = c("1125",
"157", "205", "250", "262", "264", "266", "267", "270", "296",
"305", "311", "319", "320", "324", "327", "342", "347", "350",
"353", "364", "371", "374", "375", "378", "379", "459", "463",
"591", "729", "754", "761", "879", "902", "903", "907", "911",
"913", "916", "917", "922", "931", "944", "955", "974", "985",
"FULL"), class = "factor"), Drury = c(148, 143, 147, 182, 193,
235), `Brown Thomas` = c(230, 231, 0, 267, 272, 293), `Date & Time` = structure(1:6, .Label = c("2019-03-19 13:43:33",
"2019-03-19 13:55:39", "2019-03-19 14:07:35", "2019-03-19 15:45:02",
"2019-03-19 16:00:02", "2019-03-19 16:45:03", "2019-03-19 17:00:02",
"2019-03-19 17:45:03", "2019-03-19 18:00:01", "2019-03-19 18:00:02",
"2019-03-19 18:45:03", "2019-03-19 19:00:01", "2019-03-19 19:00:02",
"2019-03-19 19:07:12", "2019-03-19 19:45:03", "2019-03-19 20:00:01",
"2019-03-19 20:00:02", "2019-03-19 20:45:03", "2019-03-19 21:00:01",
"2019-03-19 21:00:03", "2019-03-19 21:45:04", "2019-03-19 22:00:01",
"2019-03-19 22:00:03", "2019-03-19 22:45:04", "2019-03-19 23:00:01",
"2019-03-19 23:00:02", "2019-03-19 23:00:03", "2019-03-19 23:45:04",
"2019-03-20 00:00:01", "2019-03-20 00:00:02", "2019-03-20 00:00:03",
"2019-03-20 00:45:04", "2019-03-20 01:00:01", "2019-03-20 01:00:02",
"2019-03-20 01:00:03", "2019-03-20 01:45:04", "2019-03-20 02:00:01",
"2019-03-20 02:00:02", "2019-03-20 02:00:03", "2019-03-20 02:45:04",
"2019-03-20 03:00:01", "2019-03-20 03:00:02", "2019-03-20 03:00:03",
"2019-03-20 03:45:05", "2019-03-20 04:00:01", "2019-03-20 04:00:02",
"2019-03-20 04:00:04", "2019-03-20 04:45:05", "2019-03-20 05:00:01",
"2019-03-20 05:00:02",
Thanks.
First issue... if you want to explicity set all empty cells as NA you can have a custom function like the following:
empty_as_na <- function(x){
if("factor" %in% class(x)) x <- as.character(x) ## since ifelse wont work with factors
ifelse(as.character(x)!="", x, NA)
}
And then apply this function:
dplyr::mutate_all(df, .funs = empty_as_na)
where df is your data frame.
Second issue... for removing duplicate rows, you should look at dplyr::distinct()
Third issue... I did not get what the issue is... maybe you could clarify?
I am sorry, I can't give you a complete working example with the data you provided... but these functions should get you where you want.
EDIT
Solution for third issue based on comments...
Probably not the most elegant solution, but again, this is limited due to the not provided reprex.
Let df be your data frame, column_new your new column, column_number the column you mentioned that had numbers or FULL written, and column_carthe column where the cars are.
df %>%
mutate(
column_new = case_when(
column_number == "FULL" & column_car == "car_a" ~ 300,
column_number == "FULL" & column_car == "car_b" ~ 500,
TRUE ~ column_number
)
)

Change color of a specific coordinate

I want to change the color of a certain coordinate, actually is the same coordinate which has the annotation.
Any ideas?
p1 <- ggplot(HiBAP1517, aes(BPM, Yld)) +
geom_point(shape=16) +
geom_smooth(method=lm, se = F) +
theme(axis.title.x = element_text(color="black", size=14, face="bold"),
axis.title.y = element_text(color="black", size=14, face="bold"))
p2 <- p1 +
annotate(geom="text", x=1879, y=892.02, label="Rialto",
color="darkorange", size = 5, hjust=1, vjust=1.3, fontface =2)
p3 <- p2 +
annotate(geom="text", x=1654.75, y=834.2375, label="Savannah",
color="firebrick1", size = 5, hjust=1, vjust=1, fontface =2)
pfinal <- p3 +
labs(x = expression("AGDM"[PM]^{}*(gm^{-2})),
y = expression("GY"*(gm^{-2})))
This is my output, but I would like to change the color (instead of black) of those 2 coordinates:
Data sample:
Genotype,BPM,Yld
1,1767.793447,747.0708034
2,2074.815941,775.8880562
3,2197.933995,854.3810136
5,2085.627286,845.9306447
6,1908.97774,841.4318038
7,2120.24666,875.5534429
8,2226.617509,764.3849451
9,2035.68002,810.2658242
10,2153.727,861.7024631
11,1993.568134,782.5763292
12,2013.199982,822.6565187
13,2078.275912,837.2819632
14,2042.456487,802.6913977
16,1840.058841,767.6509829
17,2013.338146,801.2064103
18,2087.151352,822.1910199
19,1988.038384,859.573342
20,2083.092896,887.2783898
21,2072.905795,861.3044422
23,1849.744525,723.5014595
24,1785.04038,747.4940519
25,2078.402869,835.7669124
26,1698.390774,681.256732
27,2065.842661,852.3073467
28,2020.285009,811.6889063
29,2039.137248,821.7951099
30,1855.665106,781.0350726
31,1792.32475,744.9001931
32,1992.616447,860.7054072
33,2025.79755,834.1452611
34,2023.274784,835.4102703
35,1703.837196,682.9995098
36,1740.44177,713.3121368
37,1970.331012,816.5239645
38,1990.223669,838.9949534
39,2081.559891,822.5936391
40,1968.990856,852.1259441
41,2178.322511,920.80226
42,1887.572381,721.0746569
43,2103.964882,821.6521912
44,2097.040605,873.0062511
45,1864.779016,755.1746154
46,1935.743565,895.4951282
47,2191.797365,888.7284615
48,1968.150754,863.7490909
49,1858.735915,759.7144347
50,1933.34954,774.4202087
51,1680.540128,717.2402198
52,1748.214736,783.3395385
53,2183.694734,855.5897436
54,2142.662802,912.635349
55,1892.205584,776.5070164
56,2230.304238,887.8378102
57,2141.882287,903.7212821
58,1983.755009,815.5541958
59,1954.653032,743.0290819
60,1801.192428,718.5391635
61,1920.709571,808.6727692
62,1796.291216,699.0526007
63,2026.074655,909.3961954
64,1863.574774,729.9547929
65,1924.971832,770.2818388
66,2129.910527,794.0297343
67,2090.201938,809.6094569
68,1987.074651,731.8146606
69,2053.104282,839.4181954
70,1872.403668,787.2339391
71,1961.144455,824.335206
72,2135.414422,881.9237509
73,1857.780642,779.9428159
74,2058.696424,840.2234927
76,2169.489819,805.3868184
77,1891.844601,756.8752683
78,2099.708756,830.6765073
79,1976.981377,786.4878009
81,1932.909878,800.0033701
82,2101.603045,834.2990498
83,1867.872044,735.4201911
84,1870.947954,703.6186056
85,2135.962836,798.3315211
86,1859.497846,762.135947
87,1966.35974,776.6730353
88,2088.086246,808.0767316
89,1964.134743,851.5441764
90,2211.81001,866.3412008
91,1881.56405,805.7430148
92,1921.941058,725.2508829
93,1576.551861,606.5037422
95,2249.995426,882.4130493
96,2092.694714,778.8794369
97,2099.861152,840.9202391
98,1837.6733,760.0247786
99,1986.16533,796.1227279
100,1981.047087,747.7190033
Rialto,1879,892.02
Savannah,1654.75,834.2375
I assume you want to change the color of the points corresponding to the labels, since their precise coordinates along the x/y-axis aren't shown, and you'll have more work pinpointing them before the question about color comes up. If that's not the case, I'll delete my answer.
# define color corresponding to each genotype
HiBAP1517$color <- case_when(HiBAP1517$Genotype == "Savannah" ~ "firebrick1",
HiBAP1517$Genotype == "Rialto" ~ "darkorange",
TRUE ~ "black")
# plot
ggplot(HiBAP1517,
# specify color aesthetic here for both geom_point & geom_text to inherit
aes(x = BPM, y = Yld, color = color)) +
geom_point(shape = 16) +
geom_smooth(color = "#3366FF", # maintain default color for geom_smooth line
method = lm, se = F) +
# position labels based on their coordinates, rather than hard-code them via annotate()
geom_text(aes(label = ifelse(Genotype %in% c("Savannah", "Rialto"),
Genotype, "")),
hjust = 1, vjust = 1) +
# use defined colors directly
scale_color_identity() +
# other aesthetic parameters, irrelevant to the question at hand
labs(x = expression("AGDM"[PM]^{}*(gm^{-2})),
y = expression("GY"*(gm^{-2}))) +
theme_classic() +
theme(axis.title.x = element_text(color="black", size=14, face="bold"),
axis.title.y = element_text(color="black", size=14, face="bold"))
Data used:
> dput(HiBAP1517)
structure(list(Genotype = c("1", "2", "3", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "16", "17", "18", "19", "20",
"21", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
"33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
"44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54",
"55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65",
"66", "67", "68", "69", "70", "71", "72", "73", "74", "76", "77",
"78", "79", "81", "82", "83", "84", "85", "86", "87", "88", "89",
"90", "91", "92", "93", "95", "96", "97", "98", "99", "100",
"Rialto", "Savannah"), BPM = c(1767.793447, 2074.815941, 2197.933995,
2085.627286, 1908.97774, 2120.24666, 2226.617509, 2035.68002,
2153.727, 1993.568134, 2013.199982, 2078.275912, 2042.456487,
1840.058841, 2013.338146, 2087.151352, 1988.038384, 2083.092896,
2072.905795, 1849.744525, 1785.04038, 2078.402869, 1698.390774,
2065.842661, 2020.285009, 2039.137248, 1855.665106, 1792.32475,
1992.616447, 2025.79755, 2023.274784, 1703.837196, 1740.44177,
1970.331012, 1990.223669, 2081.559891, 1968.990856, 2178.322511,
1887.572381, 2103.964882, 2097.040605, 1864.779016, 1935.743565,
2191.797365, 1968.150754, 1858.735915, 1933.34954, 1680.540128,
1748.214736, 2183.694734, 2142.662802, 1892.205584, 2230.304238,
2141.882287, 1983.755009, 1954.653032, 1801.192428, 1920.709571,
1796.291216, 2026.074655, 1863.574774, 1924.971832, 2129.910527,
2090.201938, 1987.074651, 2053.104282, 1872.403668, 1961.144455,
2135.414422, 1857.780642, 2058.696424, 2169.489819, 1891.844601,
2099.708756, 1976.981377, 1932.909878, 2101.603045, 1867.872044,
1870.947954, 2135.962836, 1859.497846, 1966.35974, 2088.086246,
1964.134743, 2211.81001, 1881.56405, 1921.941058, 1576.551861,
2249.995426, 2092.694714, 2099.861152, 1837.6733, 1986.16533,
1981.047087, 1879, 1654.75), Yld = c(747.0708034, 775.8880562,
854.3810136, 845.9306447, 841.4318038, 875.5534429, 764.3849451,
810.2658242, 861.7024631, 782.5763292, 822.6565187, 837.2819632,
802.6913977, 767.6509829, 801.2064103, 822.1910199, 859.573342,
887.2783898, 861.3044422, 723.5014595, 747.4940519, 835.7669124,
681.256732, 852.3073467, 811.6889063, 821.7951099, 781.0350726,
744.9001931, 860.7054072, 834.1452611, 835.4102703, 682.9995098,
713.3121368, 816.5239645, 838.9949534, 822.5936391, 852.1259441,
920.80226, 721.0746569, 821.6521912, 873.0062511, 755.1746154,
895.4951282, 888.7284615, 863.7490909, 759.7144347, 774.4202087,
717.2402198, 783.3395385, 855.5897436, 912.635349, 776.5070164,
887.8378102, 903.7212821, 815.5541958, 743.0290819, 718.5391635,
808.6727692, 699.0526007, 909.3961954, 729.9547929, 770.2818388,
794.0297343, 809.6094569, 731.8146606, 839.4181954, 787.2339391,
824.335206, 881.9237509, 779.9428159, 840.2234927, 805.3868184,
756.8752683, 830.6765073, 786.4878009, 800.0033701, 834.2990498,
735.4201911, 703.6186056, 798.3315211, 762.135947, 776.6730353,
808.0767316, 851.5441764, 866.3412008, 805.7430148, 725.2508829,
606.5037422, 882.4130493, 778.8794369, 840.9202391, 760.0247786,
796.1227279, 747.7190033, 892.02, 834.2375)), class = "data.frame", row.names = c(NA,
-96L))

Add background to ggplot2 plot based on third variable

I would like to add a background to the following plot, based on a third variables (variable p in the data below). However, I don't get to make it work. There are some previous related post like for instance this one. However, it does not seem to work in my case.
THE DATA
out121<-structure(list(MEp_R = c(-0.00798875313192999, -0.0148676123843391,
-0.00682061802389229, -0.00342320747900698, -0.0151338635205871,
-0.0107317027491587, -0.0251293011821259, -0.00442888716467913,
-0.00336413112985843, -0.00684252019402579, -0.0179441683421641,
-0.0183345869013467, -0.00648428720725909, -0.0214876248035767,
-0.016436676184706, -0.0374053532385283, -0.0149373698200334,
-0.00812201774657575, -0.00845365671158477, -0.00770113356603315,
-0.0235236091649431, -0.00319728180470263, -0.00375490467048723,
-0.0105497059284769, -0.0113949568001174, -0.00796452607813328,
-0.018162596356658, -0.0305161849884928, -0.0182695024428088,
-0.00733874764247203, -0.0258767466756409, -0.00800032458837883,
-0.0254092616701198, -0.00796500399596014, -0.018098828213091,
-0.00403853247796117, -0.00537848845812282, -0.0150529001843169,
-0.0184434922700636, -0.028401757025326, -0.00394247092784099,
-0.00389984372897549, -0.0212293138913338, -0.00323995037136412,
-0.00435171103575741, -0.0142661346880167, -0.00763574799777637,
-0.0243480163192293, -0.0334550531174489, -0.00398665257789114,
-0.0269010788141356, -0.0339462390051263, -0.00731023368759553,
-0.00273217917938973, -0.00721679095419613, -0.0284960447574926,
-0.0256689850381392, -0.00418816161208601, -0.0207469376148546,
-0.0159256736739084, -0.00313049343988148, -0.0280083118773398,
-0.00305634712789695, -0.00222991072333599, -0.00728714579423012,
-0.00807506060034409, -0.0152635924760586, -0.0332733629111364,
-0.00760293035610356, -0.0139815979579726, -0.00677123700835164,
-0.0215960340688805, -0.0338156227799011, -0.0253065053249637,
-0.018647649832361, -0.0354963987663701, -0.022253017915301,
-0.00671809517372551, -0.00732452244298519, -0.0319724784878375,
-0.0342471218743405, -0.0350620385653106, -0.0230236881980878,
-0.0346963784053919, -0.00282689342327851, -0.00325484604261175,
-0.033849668042083, -0.00413139898221564, -0.0228993329834933,
-0.00559908152238757, -0.0183537460976775, -0.00472047771208315,
-0.0251909804124543, -0.0311011436015412, -0.0229936133519116,
-0.00308542036103775, -0.0228912921993044, -0.012939181841082,
-0.0230877632656367, -0.032591064432755, -0.00735469284998116,
-0.0303916947589657, -0.00314093237962542, -0.0231490730231191,
-0.0054652608506885, -0.025241520518079, -0.00445136785085343,
-0.0128593850454578, -0.00673762742746708, -0.0226916858622337,
-0.0161806428228128, -0.0209040965403646, -0.020750711338677,
-0.0181057154876848, -0.0178080592822426, -0.0176328580881027,
-0.0176110802336996, -0.0176155620180982, -0.0178561500821362,
-0.0154770868242239, -0.0155229482583411, -0.0180674673481902,
-0.0178800497751207, -0.0154828650185023, -0.0154750397213182,
-0.0152368261892578, -0.0154977482766045, -0.0182346532616592,
-0.0183274537639035, -0.0168011783244315, -0.0168016019401702,
-0.0149946562681369, -0.0158317352920475, -0.0167520543181098,
-0.0166157299693747, -0.0158279779461525, -0.0149977762443961,
-0.0180750003460435, -0.0151529841212906, -0.017923432133514,
-0.017928916003054, -0.0179127560726451, -0.0180570721490242,
-0.0177899027947602, -0.0158531006505291, -0.0174593473238637,
-0.0151291673194569, -0.0179473924690324, -0.017693119507179,
-0.0177884007129345, -0.0148359633238028, -0.0178372767093443,
-0.0179573607500675, -0.0178478763689906, -0.0159047471769866,
-0.0179332447429962, -0.0148569280408044, -0.0179181675050371,
-0.0166326656412793, -0.0148692975727, -0.0159248001099628, -0.0181012191592349,
-0.0178451421528243, -0.0165514761611982, -0.0157213399905123,
-0.0157870112143077), MEp_Irr = c(0.0267063409682918, 0.0348657385506534,
0.0342691355007968, -0.115640975842031, 0.0387492569408993, 0.0381593243875236,
0.0427698849005908, -0.0173766138908777, -0.137295803791003,
0.0255837365606775, 0.033333234010886, 0.04219733855297, 0.0283972511296412,
0.042063245938688, 0.0381434891186468, 0.116843385842082, 0.0303337697359422,
0.0270649725588233, 0.0408800022550086, 0.0212509208656095, 0.0411728527295687,
-0.124549799025409, -0.0144478716604061, 0.0423901861335305,
0.0415985024073151, 0.037704775866327, 0.0422604508495823, 0.0763588031748094,
0.0426357961926886, 0.0329608493967286, 0.0496266735787044, 0.040016714764635,
0.0519859953665068, 0.0394245716448228, 0.0342680692030386, -0.0267193515702238,
-0.0177971867890813, 0.0381256479675564, 0.0355367325792796,
0.0727607935148329, -0.0326388375297887, -0.0178079477006394,
0.0410572010103166, -0.117374678254156, -0.020320609244612, 0.0370392597214022,
0.0353728091272744, 0.0449625606410676, 0.0753850352272533, -0.0343858665221561,
0.0698051617341533, 0.077951540991422, 0.0237442438741627, -0.058297571549435,
0.0243284890185037, 0.0772458962449727, 0.0543866044441634, -0.00428096536358291,
0.04038564508393, 0.0354424565177792, -0.131352692076439, 0.0720546321152786,
-0.0665837597449708, -0.127968519873726, 0.0330986089266455,
0.0259981472748394, 0.0335310580145865, 0.0796945930317802, 0.020663774953603,
0.0293980728446607, 0.0362259277862459, 0.0432046089419517, 0.0685995447124759,
0.0469963521357076, 0.0354789715561056, 0.0709742762976496, 0.0452791300187091,
0.0331219798716994, 0.0295434908925642, 0.08744851179291, 0.0799768054034634,
0.0815503850718988, 0.0440574836496075, 0.0839865621030131, -0.0688754800689186,
-0.116779517177347, 0.0694952520459083, -0.0264949177920162,
0.0413932242242241, 0.0172278601886947, 0.0420753996022112, -0.144255178922228,
0.0470894777847475, 0.076135949143235, 0.041744283972074, -0.0630340966211796,
0.0413829074872194, 0.0396332210621789, 0.041111622411323, 0.102474470476636,
0.0325757173791441, 0.0760653479136575, -0.123211363037203, 0.0410342604744772,
-0.0174831909563214, 0.0530941558728644, -0.0418843636393472,
0.038435679009942, 0.0336208563560449, 0.0399056440442261, 0.0265540871197283,
0.0444367528340963, 0.0435895606359242, 0.0215615450224718, 0.0209324963084151,
0.0202690410290722, 0.0194304147117461, 0.0197015376925802, 0.0232944851025464,
0.0194971575601002, 0.0195128222684842, 0.0229793934050297, 0.022601136427616,
0.020063571212835, 0.0199659939198503, 0.0189019086482273, 0.020421297722678,
0.0238829270946739, 0.0242153520276344, 0.0261331103214652, 0.0259380402516094,
0.0195777353030817, 0.0234436006049973, 0.0253584791696736, 0.025820695095831,
0.0242819014695358, 0.0195352848772439, 0.0287263051849994, 0.0195826041035442,
0.025491942513768, 0.0244647468662558, 0.0257233178691396, 0.0262291526865683,
0.0247672209152699, 0.0245726192470798, 0.0273106213867739, 0.0199937372665979,
0.027112767769544, 0.027267180444377, 0.0261432756569257, 0.0198848926943087,
0.0256279615107693, 0.0279085914888886, 0.0285149093762463, 0.0240689981341775,
0.0266901583531034, 0.0200700646179756, 0.0262578329932839, 0.0263488901038499,
0.0201551487864926, 0.0244667611324202, 0.0262710081046724, 0.0264923641418946,
0.0252091848629891, 0.0230331062657154, 0.0230721882488657),
p = 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, 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, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.33226762955019e-15,
1.99840144432528e-15, 1.31006316905768e-14, 2.42694753183059e-13,
3.68407526707415e-10, 1.61860724823271e-08, 1.27863204824052e-06,
2.38962085785666e-06, 4.89028683459125e-06, 1.29586352866085e-05,
1.3194564602248e-05, 5.02157467259234e-05, 6.66173260168712e-05,
0.000452773851216115, 0.00058440281230121, 0.000733510241929469,
0.000880660946449119, 0.0136063339510712, 0.0151237168548795,
0.0353698786443037, 0.0615641433741911, 0.0627726749529298,
0.0763356770320813, 0.0824154857507584, 0.0929893345348154,
0.0947385432373617, 0.0958710673877281, 0.104039435798143,
0.117891950799349, 0.137872040413549, 0.173995516565528,
0.216611706295959, 0.277748137930015, 0.29066025684483, 0.294964122909925,
0.360934324888994, 0.390659280716865, 0.399257628135196,
0.415075121018395, 0.415906223140706, 0.429535524186567,
0.4346739056448, 0.461459259127042, 0.492938667179821, 0.552802494760443,
0.606427873500538, 0.608777827159917, 0.621809390011274,
0.65212833158726, 0.720281808796569, 0.725498755645982, 0.732029138010702,
0.873071745150822, 0.902596817430848, 0.965286951991386,
0.979065101832578)), datalabel = "Written by R. ", time.stamp = " 6 Aug 2017 22:23", .Names = c("MEp_R",
"MEp_Irr", "p"), formats = c("%9.0g", "%9.0g", "%9.0g"), types = c(255L,
255L, 255L), val.labels = c("", "", ""), var.labels = c("MEp_R",
"MEp_Irr", "p"), row.names = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50",
"51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61",
"62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72",
"73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83",
"84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94",
"95", "96", "97", "98", "99", "100", "101", "102", "103", "104",
"105", "106", "107", "108", "109", "110", "111", "112", "113",
"114", "115", "116", "117", "118", "119", "120", "121", "122",
"123", "124", "125", "126", "127", "128", "129", "130", "131",
"132", "133", "134", "135", "136", "137", "138", "139", "140",
"141", "142", "143", "144", "145", "146", "147", "148", "149",
"150", "151", "152", "153", "154", "155", "156", "157", "158",
"159", "160", "161", "162", "163", "164", "165", "166"), version = 12L, class = "data.frame")
THE PLOT
out121$perc1<-c(1:166)
l64<-ggplot(out121,aes(perc1))
l65<-l64+geom_line(aes(y=MEp_R,colour="var1"),size=1.3)+
geom_line(aes(y=MEp_Irr,colour="var2"),size=1.3)+
scale_colour_manual(values=c("blue3","green"), name="MEt")+
scale_x_discrete(name="Threshold (in percentage)",breaks=c(0, 250, 500,750,1000),
labels=c("0", "25", "50","75","100")) +
scale_y_continuous(name="MEp",limits = c(-0.7, 0.5),breaks=c(-0.3,-0.1,0,0.1,0.3,0.5))
l66<-l65+ theme_bw()+ggtitle("Full sample ") +
theme(plot.title = element_text(lineheight=.8, face="bold"),legend.position="bottom")+
guides(col=guide_legend(ncol=2,title.position="top"))+
theme(panel.background = element_rect(fill = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
THE QUESTION
I now would like to add a background to this plot, based on variable "p".
The variable only has a few values bigger than 0.05. So only for those points the background should be coloured.
That means, points lower than 0.05 should have a white background, the other points should have a red (for instance) background.
I made some unfortunate efforts based on the following code but I don't get the scaling (white/blue right, not even on a continuous basis) and I don't get the color in the background. Could somebody help me please? Thank you very much!
geom_rect(aes(xmin = 0, xmax = 1000, ymin = -Inf, ymax = Inf,fill = p))+
scale_color_gradient2(midpoint=0.05, low="white", mid="light blue",high="blue", space ="Lab" )
One of the errors I for instance get is:
"Scale for 'colour' is already
present. Adding another scale for
'colour', which will replace the
existing scale." But even if that is solved, it doesn't solve the fact that I only have one color on my background graph.
First
out121$p_above <- out121$p > 0.05
and then
l66 + geom_rect(aes(xmin = perc1 - 0.5, xmax = perc1 + 0.5,
ymin = -Inf, ymax = Inf, fill = p_above), alpha = 0.4) +
scale_fill_manual(values = c("white", "red"))

apply the same mtext to every page

I have multiple plots across multiple pages, how do I provide the same mtext for all page? below is an example data-set:
bootData <- lapply(seq(1,24), function(x) rnorm(50))
names(bootData) <- c("0", "2", "4", "6", "8", "10", "15", "20", "30", "40", "45",
"50", "55", "60", "65", "70", "80", "85", "90", "92", "94", "96",
"98", "100")
Plot the data
pdf(file = "test.pdf", width = 11.5, height = 8.5)
par(mar=c(5,4,3,2), oma=c(3,3,1,3), mfrow = c(2,3))
lapply(names(bootData), function(x) hist(bootData[[x]], main = paste("Histogram of", x)))
mtext("Error per label", side = 1, line = 1, outer = TRUE, col = "firebrick")
dev.off()
I would like the mtext "Error per label" to appear in every page. With the above code it only appears in the last page.
Many thanks in advance.
pdf(file = "test.pdf", width = 11.5, height = 8.5)
par(mar=c(5,4,3,2), oma=c(3,3,1,3), mfrow = c(2,3))
lapply(names(bootData), function(x){
hist(bootData[[x]], main = paste("Histogram of", x))
mtext("Error per label", side = 1, line = 1, outer = TRUE, col = "firebrick")
})
dev.off()

Resources