barplot does not work with a numerical matrix - r

I am trying to make a simple barplot with the data shown below
xx <- structure(list(13L, 65L, 569L, 452L, 7L, 11L, 58L, 134L, 18L,
17L, 4L, 18L, 93L, 0L, 0L, 2L, 1L, 50L, 135L, 7L, 88L, 226L,
179L, 0L, 0L, 44L, 121L, 17L, 191L, 1L), .Dim = 5:6, .Dimnames = list(
c("minus_2_H", "minus_H", "no_adduct", "plus_H", "plus_2H"
), c("a", "b", "c", "x", "y", "z")))
however it only works for the
barplot (xx)
and fails with
barplot (xx, beside = TRUE)
Error in -0.01 * height : non-numeric argument to binary operator
Could anyone clarify why its complaining even though the matrix is numeric in both cases?

Related

magrittr pipe to righthand side in braces gives error

I am using the magrittr pipe %>% to pipe an object into multiple arguments in the righthand side function, using curly braces to force :
data_bec <- dds_tmp %>%
{sva::ComBat_seq(
counts = . %>% SummarizedExperiment::assay(),
batch = . %>% magrittr::extract2("batch") %>% droplevels(),
covar_mod = cbind(
time = . %>% magrittr::extract2("time") %>% droplevels(),
treatment = . %>% magrittr::extract2("treatment") %>% droplevels()
)
)}
Which gives this error:
Error in unique.default(x, nmax = nmax) :
unique() applies only to vectors
However, the code below works:
data_bec <- dds_tmp %>%
{sva::ComBat_seq(
counts = SummarizedExperiment::assay(.),
batch = magrittr::extract2(., "batch") %>% droplevels(),
covar_mod = cbind(
time = magrittr::extract2(., "time") %>% droplevels(),
treatment = magrittr::extract2(., "treatment") %>% droplevels()
)
)}
I can't wrap my head around why magrittr::extract2(., "batch") %>% droplevels() works inside a function inside braces, but . %>% magrittr::extract2("batch") %>% droplevels() doesn't work. Is there an easy way to understand this behavior?
NB: My dds_tmp object is large and produced by DESeq2::sample_table. Here is a small example data set:
dds_tmp <- new(
"DESeqDataSet",
design = ~ 1,
dispersionFunction = function ()
NULL,
rowRanges = new(
"CompressedGRangesList",
unlistData = new(
"GRanges",
seqnames = new(
"Rle",
values = structure(integer(0), .Label = character(0), class = "factor"),
lengths = integer(0),
elementMetadata = NULL,
metadata = list()
),
ranges = new(
"IRanges",
start = integer(0),
width = integer(0),
NAMES = NULL,
elementType = "ANY",
elementMetadata = NULL,
metadata = list()
),
strand = new(
"Rle",
values = structure(integer(0), .Label = c("+",
"-", "*"), class = "factor"),
lengths = integer(0),
elementMetadata = NULL,
metadata = list()
),
seqinfo = new(
"Seqinfo",
seqnames = character(0),
seqlengths = integer(0),
is_circular = logical(0),
genome = character(0)
),
elementMetadata = new(
"DFrame",
rownames = NULL,
nrows = 0L,
listData = structure(list(), .Names = character(0)),
elementType = "ANY",
elementMetadata = NULL,
metadata = list()
),
elementType = "ANY",
metadata = list()
),
elementMetadata = new(
"DFrame",
rownames = NULL,
nrows = 50L,
listData = structure(list(), .Names = character(0)),
elementType = "ANY",
elementMetadata = new(
"DFrame",
rownames = NULL,
nrows = 0L,
listData = list(type = character(0), description = character(0)),
elementType = "ANY",
elementMetadata = NULL,
metadata = list()
),
metadata = list()
),
elementType = "GRanges",
metadata = list(),
partitioning = new(
"PartitioningByEnd",
end = c(
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
),
NAMES = c(
"0610005C13Rik", "0610007C21Rik", "0610007L01Rik", "0610007N19Rik",
"0610007P08Rik", "0610007P14Rik", "0610007P22Rik", "0610008F07Rik",
"0610009B14Rik", "0610009B22Rik", "0610009D07Rik", "0610009L18Rik",
"0610009O20Rik", "0610010B08Rik", "0610010F05Rik", "0610010K14Rik",
"0610010O12Rik", "0610011F06Rik", "0610011L14Rik", "0610012G03Rik",
"0610012H03Rik", "0610030E20Rik", "0610031J06Rik", "0610031O16Rik",
"0610037L13Rik", "0610037P05Rik", "0610038B21Rik", "0610038L08Rik",
"0610039K10Rik", "0610040B10Rik", "0610040F04Rik", "0610040J01Rik",
"0610043K17Rik", "0910001L09Rik", "100043387", "1100001G20Rik",
"1110001A16Rik", "1110001J03Rik", "1110002B05Rik", "1110002L01Rik",
"1110002N22Rik", "1110003E01Rik", "1110004E09Rik", "1110004F10Rik",
"1110005A03Rik", "1110006O24Rik", "1110007C09Rik", "1110008F13Rik",
"1110008J03Rik", "1110008L16Rik"
),
elementType = "ANY",
elementMetadata = NULL,
metadata = list()
)
),
colData = new(
"DFrame",
rownames = c(
"IM_MR30_S30_L001", "IM_MR31_S31_L001", "IM_MR32_S32_L001",
"IM_MR33_S33_L001", "IM_MR34_S34_L001", "IM_MR35_S35_L001",
"IM_MR36_S36_L001", "IM_AR_30", "IM_AR_31", "IM_AR_32", "IM_AR_33"
),
nrows = 11L,
listData = list(
batch = structure(
c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L),
.Label = c(
"190701_K00242_0579_AH7GFLBBXY",
"180810_K00242_0453_AHVJTJBBXX-IM-AR-RS21+180814_K00242_0456_AHW5LYBBXX-IM-AR-RS21+180817_K00242_0458_AHW57TBBXX-IM-AR-RS21",
"180406_K00242_0385_AHTCCHBBXX-IM-AR-RS8+180412_K00242_0388_AHT7NYBBXX-IM-AR-RS8",
"180814_K00242_0456_AHW5LYBBXX-IM-AR-RS21+180814_K00242_0456_AHW5LYBBXX-IM-AR-RS21+180817_K00242_0458_AHW57TBBXX-IM-AR-RS21",
"190322_K00242_0534_AHYLVWBBXX_IM-AM-RS8-dT"
),
class = "factor"
),
time_label = c( "D8", "D10", "D10", "D10", "D15", "D15", "D15", "D6",
"D6", "D6", "D6"
),
time_value = c(8L, 10L,
10L, 10L, 15L, 15L, 15L, 6L, 6L, 6L, 6L),
user = c("MR",
"MR", "MR", "MR", "MR", "MR", "MR", "AR", "AR", "AR",
"AR"),
treatment = structure(
c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 1L),
.Label = c("ctrl", "OE"),
class = "factor"
),
group = structure(
c(10L, 11L, 11L, 11L, 12L, 12L, 12L,
13L, 13L, 13L, 14L),
.Label = c(
"M_2i_ctrl", "M_D0_ctrl", "M_D1_ctrl", "M_D2_ctrl", "M_D3_ctrl",
"M_D4_ctrl", "M_D5_ctrl", "M_D6_ctrl", "M_D7_ctrl", "M_D8_ctrl",
"M_D10_ctrl", "M_D15_ctrl", "A_D6_OE", "A_D6_ctrl", "A_D7_ctrl",
"A_D7_OE", "A_D8_ctrl", "A_D8_OE", "A_D11_ctrl", "A_D11_OE"
),
class = "factor"
),
group_pca_label = c(
NA, "M_D10_ctrl", NA, NA, "M_D15_ctrl", NA, NA, "A_D6_OE", NA, NA,
"A_D6_ctrl"
),
condition = structure(
c(10L,
11L, 11L, 11L, 13L, 13L, 13L, 14L, 14L, 14L, 8L),
.Label = c(
"2i_ctrl", "D0_ctrl", "D1_ctrl", "D2_ctrl", "D3_ctrl", "D4_ctrl",
"D5_ctrl", "D6_ctrl", "D7_ctrl", "D8_ctrl", "D10_ctrl", "D11_ctrl",
"D15_ctrl", "D6_OE", "D7_OE", "D8_OE", "D11_OE"
),
class = "factor"
),
time = structure(
c(10L, 11L, 11L,
11L, 13L, 13L, 13L, 8L, 8L, 8L, 8L),
.Label = c(
"2i", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D10",
"D11", "D15"
),
class = "factor"
)
),
elementType = "ANY",
elementMetadata = new(
"DFrame",
rownames = NULL,
nrows = 9L,
listData = list(
type = c(
"input", "input", "input", "input", "input", "input", "input",
"input", "input"
),
description = c("",
"", "", "", "", "", "", "", "")
),
elementType = "ANY",
elementMetadata = NULL,
metadata = list()
),
metadata = list()
),
assays = new(
"SimpleAssays",
data = new(
"SimpleList",
listData = list(counts = structure(
c(
0L, 221L, 582L, 60L, 392L, 521L, 88L, 0L, 0L, 99L, 405L, 3L, 345L,
0L, 221L, 151L, 4L, 63L, 227L, 117L, 11L, 376L, 406L, 0L, 143L,
255L, 5L, 0L, 0L, 7L, 5L, 1L, 0L, 182L, 8L, 0L, 87L, 20L, 381L, 71L,
58L, 331L, 56L, 589L, 36L, 2L, 73L, 254L, 55L, 95L, 0L, 381L, 691L,
80L, 353L, 397L, 126L, 0L, 0L, 151L, 434L, 6L, 374L, 0L, 242L, 134L,
43L, 113L, 232L, 130L, 23L, 325L, 458L, 0L, 165L, 358L, 4L, 0L, 0L,
10L, 0L, 0L, 4L, 225L, 7L, 0L, 100L, 52L, 486L, 115L, 73L, 512L,
86L, 639L, 50L, 3L, 108L, 273L, 64L, 105L, 0L, 261L, 588L, 65L,
334L, 367L, 88L, 0L, 0L, 102L, 389L, 8L, 333L, 0L, 216L, 158L, 38L,
87L, 254L, 87L, 24L, 302L, 434L, 0L, 159L, 292L, 4L, 0L, 2L, 7L, 1L,
1L, 0L, 197L, 12L, 0L, 86L, 39L, 482L, 99L, 58L, 408L, 67L, 657L,
43L, 1L, 73L, 242L, 63L, 68L, 0L, 271L, 579L, 88L, 298L, 383L, 80L,
0L, 0L, 95L, 358L, 3L, 271L, 0L, 195L, 143L, 23L, 64L, 200L, 82L,
27L, 334L, 382L, 0L, 170L, 296L, 4L, 0L, 0L, 4L, 0L, 1L, 1L, 171L,
14L, 0L, 122L, 26L, 365L, 86L, 62L, 343L, 67L, 589L, 35L, 1L, 59L,
251L, 39L, 71L, 0L, 332L, 566L, 112L, 330L, 282L, 82L, 0L, 0L, 107L,
318L, 1L, 349L, 0L, 218L, 119L, 122L, 68L, 143L, 111L, 26L, 413L,
540L, 0L, 124L, 315L, 0L, 0L, 1L, 6L, 0L, 1L, 3L, 235L, 13L, 0L,
88L, 66L, 508L, 99L, 71L, 510L, 73L, 532L, 46L, 1L, 145L, 342L, 49L,
60L, 0L, 297L, 598L, 100L, 283L, 278L, 58L, 0L, 0L, 115L, 306L, 4L,
368L, 0L, 208L, 109L, 105L, 81L, 190L, 113L, 13L, 406L, 563L, 0L,
115L, 280L, 5L, 0L, 0L, 4L, 0L, 1L, 2L, 238L, 22L, 0L, 78L, 75L,
532L, 54L, 53L, 484L, 79L, 510L, 35L, 1L, 154L, 361L, 46L, 45L, 0L,
413L, 724L, 102L, 352L, 325L, 77L, 0L, 0L, 87L, 364L, 8L, 398L, 0L,
201L, 130L, 94L, 63L, 227L, 116L, 16L, 398L, 594L, 0L, 127L, 312L,
4L, 0L, 0L, 8L, 0L, 0L, 0L, 285L, 12L, 0L, 99L, 61L, 638L, 56L, 44L,
562L, 80L, 604L, 32L, 3L, 178L, 394L, 51L, 81L, 0L, 342L, 1147L,
136L, 887L, 568L, 197L, 0L, 0L, 192L, 644L, 13L, 653L, 0L, 601L,
286L, 36L, 98L, 478L, 190L, 8L, 706L, 694L, 0L, 229L, 306L, 11L, 0L,
1L, 9L, 0L, 6L, 10L, 221L, 39L, 0L, 132L, 109L, 1132L, 350L, 133L,
567L, 115L, 931L, 61L, 5L, 70L, 561L, 130L, 133L, 0L, 337L, 1162L,
135L, 927L, 578L, 182L, 0L, 0L, 242L, 665L, 21L, 702L, 0L, 674L,
307L, 35L, 122L, 475L, 176L, 8L, 653L, 714L, 0L, 251L, 312L, 11L,
0L, 3L, 15L, 0L, 9L, 10L, 209L, 31L, 0L, 168L, 111L, 1231L, 383L,
150L, 552L, 129L, 980L, 72L, 4L, 52L, 686L, 142L, 130L, 1L, 284L,
1159L, 145L, 861L, 529L, 168L, 0L, 0L, 202L, 688L, 3L, 670L, 0L,
654L, 298L, 27L, 122L, 465L, 197L, 15L, 707L, 657L, 0L, 223L, 284L,
6L, 0L, 1L, 13L, 0L, 3L, 10L, 199L, 31L, 0L, 125L, 88L, 1142L, 397L,
121L, 593L, 128L, 888L, 60L, 7L, 54L, 595L, 117L, 150L, 0L, 224L,
1228L, 68L, 894L, 674L, 213L, 0L, 0L, 197L, 798L, 12L, 758L, 0L,
446L, 285L, 21L, 75L, 458L, 185L, 5L, 738L, 456L, 0L, 261L, 339L,
7L, 0L, 0L, 9L, 0L, 0L, 1L, 180L, 32L, 0L, 110L, 98L, 919L, 249L,
134L, 387L, 188L, 1055L, 64L, 3L, 52L, 630L, 110L, 175L
),
.Dim = c(50L, 11L),
.Dimnames = list(
c(
"0610005C13Rik", "0610007C21Rik", "0610007L01Rik",
"0610007N19Rik", "0610007P08Rik", "0610007P14Rik",
"0610007P22Rik", "0610008F07Rik", "0610009B14Rik",
"0610009B22Rik", "0610009D07Rik", "0610009L18Rik",
"0610009O20Rik", "0610010B08Rik", "0610010F05Rik",
"0610010K14Rik", "0610010O12Rik", "0610011F06Rik",
"0610011L14Rik", "0610012G03Rik", "0610012H03Rik",
"0610030E20Rik", "0610031J06Rik", "0610031O16Rik",
"0610037L13Rik", "0610037P05Rik", "0610038B21Rik",
"0610038L08Rik", "0610039K10Rik", "0610040B10Rik",
"0610040F04Rik", "0610040J01Rik", "0610043K17Rik",
"0910001L09Rik", "100043387", "1100001G20Rik", "1110001A16Rik",
"1110001J03Rik", "1110002B05Rik", "1110002L01Rik",
"1110002N22Rik", "1110003E01Rik", "1110004E09Rik",
"1110004F10Rik", "1110005A03Rik", "1110006O24Rik",
"1110007C09Rik", "1110008F13Rik", "1110008J03Rik", "1110008L16Rik"
),
c(
"IM_MR30_S30_L001", "IM_MR31_S31_L001", "IM_MR32_S32_L001",
"IM_MR33_S33_L001", "IM_MR34_S34_L001", "IM_MR35_S35_L001",
"IM_MR36_S36_L001", "IM_AR_30", "IM_AR_31", "IM_AR_32", "IM_AR_33"
)
)
)),
elementType = "ANY",
elementMetadata = NULL,
metadata = list()
)
),
NAMES = NULL,
elementMetadata = new(
"DFrame",
rownames = NULL,
nrows = 50L,
listData = structure(list(), .Names = character(0)),
elementType = "ANY",
elementMetadata = NULL,
metadata = list()
),
metadata = list(version = structure(
list(c(1L, 30L, 1L)), class = c("package_version",
"numeric_version")
))
)
We can use
data_bec <- dds_tmp %>%
{sva::ComBat_seq(
counts = {.} %>% SummarizedExperiment::assay(),
batch = {.} %>% magrittr::extract2("batch") %>% droplevels(),
covar_mod = cbind(
time = {.} %>% magrittr::extract2("time") %>% droplevels(),
treatment = {.} %>% magrittr::extract2("treatment") %>% droplevels()
)
)}

Widening Data and Changing Columns

I have managed to delete a little bit of code that did the below task and can't for the life of me figure out how I did it before.
I want to widen the data that has two factors spread over 8 different 'waves'. There are four 'Paper' factors, each with the same four internal factors 'Response'. The output from a previously required function gives the following dataframe:
[
And I would like to make it look like this:
The single column of the first tibble has become the single row of the second tibble.
As you can see, the second tibble has extra factors of Paper but these can just be joined row wise.
I really wasn't sure how to attack this, but thought it would be done using the pivot_wider function. When I tried
times_correct <- times_19 %>%
pivot_wider( id_cols = c('Stay/remain in the EU`', 'Leave the EU', 'I would/will not vote', 'Don\'t know'), names_from = eurrefcolnames)
I got the error that I can't subset columns that don't exist which makes sense: I need to manually add the correct 'Waves'. I think this is relatively simple, but can't for the life of me figure out how I did it!
Here is the dput of the various tibbles:
structure(list(resp = structure(c(3L, 2L, 4L, 1L, NA, NA, NA,
NA), .Label = c("Don't Know", "Leave", "Remain", "Will Not Vote"
), class = "factor"), `Stay/remain in the EU` = c(316L, 290L,
313L, 324L, 338L, 320L, 325L, 335L), `Leave the EU` = c(157L,
123L, 159L, 154L, 134L, 189L, 187L, 181L), `I would/will not vote` = c(2L,
3L, 3L, 3L, 2L, 2L, 2L, 0L), `Don't know` = c(56L, 51L, 55L,
50L, 57L, 20L, 17L, 0L), Paper = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "Times", class = "factor")), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"))
structure(list(resp = structure(c(3L, 2L, 4L, 1L, 3L, 2L, 4L,
1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L), .Label = c("Don't Know",
"Leave", "Remain", "Will Not Vote"), class = "factor"), euRefVoteW1 = c(316L,
157L, 2L, 56L, 190L, 339L, 4L, 70L, 819L, 79L, 9L, 71L, 1294L,
1311L, 150L, 523L, 1715L, 2587L, 133L, 630L), euRefVoteW2 = c(290L,
123L, 3L, 51L, 175L, 282L, 3L, 62L, 777L, 74L, 5L, 62L, 1091L,
925L, 80L, 371L, 1528L, 2044L, 83L, 517L), euRefVoteW3 = c(313L,
159L, 3L, 55L, 199L, 334L, 4L, 69L, 835L, 81L, 10L, 57L, 1348L,
1289L, 139L, 508L, 1766L, 2563L, 156L, 586L), euRefVoteW4 = c(324L,
154L, 3L, 50L, 215L, 328L, 2L, 61L, 848L, 70L, 10L, 55L, 1397L,
1267L, 128L, 492L, 1853L, 2494L, 143L, 583L), euRefVoteW6 = c(338L,
134L, 2L, 57L, 241L, 286L, 2L, 77L, 853L, 68L, 5L, 57L, 1519L,
1133L, 112L, 520L, 2017L, 2284L, 106L, 667L), euRefVoteW7 = c(320L,
189L, 2L, 20L, 186L, 384L, 2L, 34L, 832L, 109L, 8L, 34L, 1449L,
1456L, 87L, 292L, 1906L, 2785L, 55L, 328L), euRefVoteW8 = c(325L,
187L, 2L, 17L, 187L, 384L, 1L, 34L, 836L, 118L, 5L, 24L, 1462L,
1522L, 72L, 228L, 1898L, 2852L, 56L, 268L), euRefVoteW9 = c(335L,
181L, 0L, 0L, 206L, 385L, 0L, 6L, 844L, 102L, 0L, 4L, 1572L,
1462L, 0L, 21L, 2018L, 2827L, 0L, 20L), Paper = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L), .Label = c("Times", "Telegraph", "Control", "No_Paper",
"Rest"), class = "factor")), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
eurrefcolnames = c('euRefVoteW1','euRefVoteW2', 'euRefVoteW3', 'euRefVoteW4', 'euRefVoteW6',' euRefVoteW7', 'euRefVoteW8', 'euRefVoteW9')
EDIT:
Here is the function that create the initial dataframes, is there an edit I could make here perhaps ?
tally_reader_number <- function(input_dataframe,newspaper_name) {
#function takes the input of in_all_waves, tallies the number of different eu ref responses using map_df for a given newspaper factor (defined above)
# and returns a dataframe of responese for each wave with the newspaper factor as a column
returned_dataframe <- input_dataframe %>%
filter(Paper == newspaper_name) %>%
ungroup() %>% #function refuses to work without this
select(-Paper) %>%
map_df(table) %>% # use map_df from the purrr package to "table" each column
rownames_to_column("response") %>% #convert the rownames to a column named response
mutate(resp = case_when(response == 1 ~ "Remain", #change the resulting numbers to the correct responses
response == 2 ~ "Leave",
response ==3 ~ "Will Not Vote",
response == 4 ~ "Don't Know")) %>%
select(resp, everything(), -response) %>% #reorder the columns with resp at the front, removing response
mutate(Paper = newspaper_name)
returned_dataframe$Paper <- as.factor(returned_dataframe$Paper)
returned_dataframe$resp <- as.factor(returned_dataframe$resp)
returned_dataframe
}

order geom_point by specific facet

I have a ggplot related question, which should be easy but I could not find the answer yet. I am trying to plot a faceted plot with the code below and this dataset (11 kB).
ggplot(plot.dat, aes(x = estimate, y = reorder(countryyear, estimate))) +
geom_point() +
geom_segment(aes(x=conf.low, xend=conf.high, yend=countryyear)) +
facet_grid(. ~ facet) +
xlab("Random Effect Estimate") +
ylab("") + scale_x_continuous(breaks=c(seq(0, 5, 1)), limits=c(0, 5)) +
ggtitle("Random Slopes in Country*Year Groups from Northwestern Europe") +
theme_minimal() + theme(plot.title = element_text(hjust = 0.5))
I would like countryyear to be organized by the values of estimate in the Extreme Right facet. Not quite sure how to order by values of a specific facet. Any ideas are welcome! Thanks.
Update: Here is the dput structure of a random subset of the dataset. It has some missing values, but it should work for the sake of the example. I also updated the download link above, that has the full version.
structure(list(estimate = c(1.41056902925372, 0.854859208455895,
1.16012834593894, 0.871339033194504, 0.803272289946221, 1.17540386134493,
0.996313357490551, 1.49940694539732, 1.33773365908762, 2.7318703090905,
1.19131935418045, 1.12765907711738, 0.746741192261761, 0.985847015192172,
0.912357310925342, 1.11582763712164, 1.21854572824977, 0.675712547978394,
0.566955524699616, 1.32611743759365, 0.519648352294682, 0.591013596394243,
1.30944973684044, 0.613722269599125, 1.13293279727271, 0.950788678552604,
1.1599446923567, 1.11493952112913, 0.95336321045095, 1.39002327097034,
0.794207546872633, 0.788545101449259, 1.01096883872495, 0.897407203907834,
1.38391605229103, 1.35754760293107, 1.0718508539761, 0.542191158958878,
0.757132752456427, 1.44172863221312, 1.04842251986171, 0.77260404885379,
0.879288027642055, 1.09372353598088, 0.745484830381145, 1.21211217249353,
0.628009608902132, 1.34864488674734), countryyear = structure(c(1L,
2L, 4L, 5L, 7L, 9L, 10L, 12L, 13L, 26L, 28L, 29L, 31L, 32L, 34L,
36L, 37L, 39L, 40L, 57L, 59L, 60L, 62L, 63L, 65L, 67L, 68L, 70L,
71L, 73L, 75L, 76L, 89L, 90L, 92L, 94L, 95L, 103L, 104L, 106L,
108L, 109L, 111L, 128L, 130L, 132L, 133L, 135L), .Label = c("AT02",
"AT04", "AT06", "AT14", "AT16", "BE02", "BE04", "BE06", "BE08",
"BE10", "BE12", "BE14", "BE16", "BG06", "BG08", "BG10", "BG12",
"CH14", "CZ02", "CZ04", "CZ08", "CZ10", "CZ12", "CZ14", "CZ16",
"DE02", "DE04", "DE06", "DE08", "DE10", "DE12", "DE14", "DE16",
"DK02", "DK04", "DK06", "DK08", "DK10", "DK12", "DK14", "EE04",
"EE06", "EE08", "EE10", "EE12", "EE14", "EE16", "ES02", "ES04",
"ES06", "ES08", "ES10", "ES12", "ES14", "ES16", "FI02", "FI04",
"FI06", "FI08", "FI10", "FI12", "FI14", "FI16", "FR06", "FR08",
"FR10", "FR12", "FR14", "FR16", "GB02", "GB04", "GB06", "GB08",
"GB10", "GB12", "GB14", "GB16", "GR02", "GR04", "GR08", "GR10",
"HU02", "HU06", "HU08", "HU10", "HU12", "HU14", "HU16", "IE02",
"IE04", "IE06", "IE08", "IE10", "IE12", "IE14", "IE16", "IT04",
"IT12", "IT16", "LT10", "LT12", "LT14", "NL02", "NL04", "NL06",
"NL08", "NL10", "NL12", "NL14", "NL16", "NO14", "PL02", "PL04",
"PL06", "PL08", "PL10", "PL12", "PL14", "PL16", "PT02", "PT04",
"PT06", "PT08", "PT10", "PT12", "PT14", "PT16", "SE02", "SE04",
"SE06", "SE08", "SE10", "SE12", "SE14", "SE16", "SI02", "SI04",
"SI06", "SI08", "SI10", "SI12", "SI14", "SI16", "SK04", "SK06",
"SK08", "SK10", "SK12"), class = "factor"), facet = structure(c(1L,
3L, 1L, 4L, 5L, 3L, 4L, 1L, 1L, 1L, 5L, 5L, 4L, 5L, 3L, 1L, 2L,
4L, 5L, 2L, 1L, 4L, 2L, 5L, 2L, 3L, 4L, 3L, 2L, 5L, 5L, 4L, 2L,
5L, 4L, 5L, 3L, 1L, 4L, 5L, 3L, 5L, 4L, 1L, 5L, 2L, 4L, 1L), .Label = c("Intercept",
"Extreme Left", "Center", "Right", "Extreme Right"), class = "factor"),
conf.low = c(1.16824810706745, 0.686215051613965, 0.910277310292764,
0.591705078386698, 0.37357342399703, 0.947951001435781, 0.663296044193037,
1.18794112232166, 1.06645119085865, 2.33578182814618, 0.580210898576738,
0.564235690522211, 0.530859530342114, 0.516191258265551,
0.730992343373883, 0.862424540370486, 0.827891784352444,
0.427638276259852, 0.275692447335368, 0.829763907986328,
0.370078643492081, 0.321852705445509, 0.83550621863293, 0.289836810427436,
0.847226120408727, 0.780056160572728, 0.873143885861924,
0.869757467125519, 0.615741777890997, 0.649483531741787,
0.349657606457465, 0.523294407847395, 0.670109418373736,
0.36656743494149, 0.952201390937053, 0.777207016700884, 0.888128473009524,
0.397085597526946, 0.479828726362257, 0.614533313431094,
0.813336887981082, 0.3129232351085, 0.61435321820328, 0.854801028643867,
0.346698059397102, 0.805414039007076, 0.434676644041643,
1.07780736338027), conf.high = c(1.70315275860739, 1.06494933995261,
1.47855797769819, 1.28312522319126, 1.7272277157504, 1.45743211956315,
1.49652679976667, 1.8925358720741, 1.67802460909168, 3.19512520208851,
2.44607918797515, 2.25369471581694, 1.05041423643869, 1.8828182806291,
1.13872035780431, 1.44368725318228, 1.79353596677755, 1.06769546329854,
1.16593171156554, 2.11938292490653, 0.729667639003753, 1.08526995489865,
2.05223919950836, 1.29954170985538, 1.51498719434776, 1.15888977865399,
1.54095070825389, 1.4292376699955, 1.47610807594453, 2.97492484321718,
1.80395225460704, 1.18824770090216, 1.52521060717706, 2.19697554354282,
2.01136404338166, 2.37122858469145, 1.29357889999432, 0.740322123703373,
1.19469713534712, 3.38237391450413, 1.35145693795059, 1.90755095606211,
1.25847381058047, 1.39942645489832, 1.60297301142912, 1.82417470710871,
0.907332092210651, 1.68753999308876)), row.names = c(1L,
9L, 17L, 25L, 33L, 41L, 49L, 57L, 65L, 128L, 136L, 144L, 152L,
160L, 168L, 176L, 184L, 192L, 200L, 283L, 291L, 299L, 307L, 315L,
323L, 331L, 339L, 347L, 355L, 363L, 371L, 379L, 442L, 450L, 458L,
466L, 474L, 512L, 520L, 528L, 536L, 544L, 552L, 640L, 648L, 656L,
664L, 672L), class = "data.frame")

ggplot2: Inconsistent color from alpha

I am making several plots that have different x-axis limits, and I want to highlight a region of interest by adding a grey box. Even though I use the same geom_rect() command with the same alpha value in ggplot2, I get results with very different grey colors. I have looked here and here but so far have not figured out how to make these boxes the same level of transparency. Below is a reproducible example (with fake data) and the figures that it produces. Notice the different color of the grey rectangles. I want the grey to be the same across plots.
Data<-structure(list(X = c(34L, 27L, 28L, 47L, 26L, 3L, 13L, 31L, 39L,
16L, 45L, 5L, 49L, 17L, 29L, 43L, 1L, 35L, 41L, 10L, 48L, 24L,
12L, 11L, 30L, 40L, 8L, 4L, 20L, 25L, 50L, 22L, 9L, 21L, 18L,
7L, 15L, 44L, 6L, 36L, 46L, 33L, 2L, 37L, 23L, 14L, 42L, 38L,
19L, 32L, 34L, 27L, 28L, 47L, 26L, 3L, 13L, 31L, 39L, 16L, 45L,
5L, 49L, 17L, 29L, 43L, 1L, 35L, 41L, 10L, 48L, 24L, 12L, 11L,
30L, 40L, 8L, 4L, 20L, 25L, 50L, 22L, 9L, 21L, 18L, 7L, 15L,
44L, 6L, 36L, 46L, 33L, 2L, 37L, 23L, 14L, 42L, 38L, 19L, 32L
), Y = c(130L, 146L, 58L, 110L, 117L, 135L, 133L, 108L, 97L,
61L, 71L, 64L, 103L, 142L, 125L, 104L, 100L, 147L, 111L, 78L,
56L, 145L, 62L, 69L, 70L, 116L, 137L, 79L, 150L, 94L, 91L, 81L,
65L, 118L, 129L, 83L, 98L, 84L, 85L, 148L, 93L, 73L, 59L, 87L,
134L, 88L, 136L, 90L, 140L, 55L, 89L, 115L, 123L, 51L, 132L,
126L, 66L, 80L, 60L, 120L, 109L, 76L, 74L, 57L, 149L, 121L, 138L,
128L, 114L, 127L, 68L, 107L, 67L, 112L, 144L, 119L, 53L, 52L,
54L, 96L, 131L, 106L, 113L, 72L, 95L, 63L, 92L, 86L, 75L, 105L,
82L, 101L, 139L, 143L, 122L, 77L, 99L, 141L, 124L, 102L), B = structure(c(2L,
2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 1L, 1L), class = "factor", .Label = c("no", "yes"))), .Names = c("X",
"Y", "B"), row.names = c(NA, -100L), class = "data.frame")
Data2<-structure(list(variable = c(2.49676547444708, 0.67359598601097,
0.674751772966082, 0.0317590441796792, 0.485143583939748, 1.08231639527806,
0.0732344181040914, 1.62357048819912, 0.146833215667032, 0.823157103468943,
0.240761579418538, 1.37540376416553), DOY_mid_month = c(15, 46,
75, 106, 136, 167, 197, 228, 259, 289, 320, 350)), .Names = c("variable",
"DOY_mid_month"), row.names = c(NA, -12L), class = "data.frame")
test<-ggplot(data=Data) +
geom_rect(aes(xmin=5, xmax=30, ymin=1, ymax=40), alpha = 0.02) +
geom_point(aes(x = X, y = X, colour= B), data =Data, size=2) +
theme_bw()
test2 <-ggplot(data=Data2) +
geom_rect(aes(xmin=5, xmax=30, ymin=-Inf, ymax=Inf), alpha = 0.02) +
geom_point(aes(x = DOY_mid_month, y = variable), color="black", size=4) +
scale_x_continuous("Day of Year", limits = c(0, 366)) + # Use this to add back X-axis label for the bottom plot in panel
scale_y_continuous(expression(paste("Variable", sep=""))) +
theme_bw()
Plot result from first example:
Plot result from second example:
You are currently drawing one rectangle for each row of the dataset. The more rectangles you overlap, the darker they get, which is why the longer dataset has a darker rectangle. Use annotate instead of geom_rect to draw a single rectangle.
annotate(geom = "rect", xmin=5, xmax=30, ymin=-Inf, ymax=Inf, alpha = 0.2)
If you want to stick with geom_rect you can give a one row data.frame to that layer so that each rectangle is only drawn one time. Here I use a fake dataset, although you could put your rectangle limits in the data.frame, as well.
geom_rect(data = data.frame(fake = 1),
aes(xmin = 5, xmax= 30, ymin = -Inf, ymax = Inf), alpha = 0.2)

Set ggmap boundary based on Latitude and Longitude

So I have R program, and am struggling with getting all points in map
library(ggmap)
library(ggplot2)
setwd("d:/GIS/")
sep <- read.csv("SEP_assets_csv.csv")
Sub1 <- sep[grep("SEP.12", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])
# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] >= 50, "Over 50", "Under 50")
# get the map
map <- get_map("Kissena Park, Queens", zoom = 13, maptype = 'roadmap')
# plot the map and use the grouping variable for the fill inside the aes
ggmap(map) +
geom_point(data=sep, aes(x = Longitude, y = Latitude, color=Percent_SEP12_Assets ), size=9, alpha=0.6) +
scale_color_manual(breaks=c("Over 50", "Under 50"), values=c("green","red"))
And here is output map
I wish to zoom in enough without cutting out data points, but no matter location I pick on map, the data keeps getting cut, i.e. Removed 2 rows containing missing values (geom_point).
Is there a way to set boundaries based on the extremities of latitude and longitude? The csv I import at
sep <- read.csv("SEP_assets_csv.csv")
Has list of latitude and longitude.
Help!
Coordinates
Latitude Longitude
40.758365 -73.824407
40.774168 -73.818543
40.761748 -73.811379
40.765602 -73.828293
40.751762 -73.81778
40.764834 -73.789712
40.777951 -73.842932
40.76501 -73.794319
40.785959 -73.817349
40.755764 -73.799256
40.745593 -73.829283
40.789929 -73.839501
40.760072 -73.783908
40.726437 -73.807592
40.741093 -73.808757
40.720926 -73.823358
40.729642 -73.81781
40.724191 -73.80937
40.782346 -73.77844
40.778164 -73.799841
40.775122 -73.8185
40.760344 -73.817909
40.792326 -73.809516
40.78322 -73.806977
40.73106 -73.805449
40.736521 -73.813001
40.783714 -73.795027
40.770194 -73.82762
40.735855 -73.823583
40.74943 -73.82141
40.769753 -73.832001
40.754465 -73.826204
40.738775 -73.823892
40.764868 -73.826819
40.738332 -73.82028
40.735017 -73.821339
40.72535 -73.811325
40.721466 -73.820401
dput
> dput(sep)
structure(list(School = structure(1:38, .Label = c("Queens\\25Q020",
"Queens\\25Q021", "Queens\\25Q022", "Queens\\25Q023", "Queens\\25Q024",
"Queens\\25Q025", "Queens\\25Q029", "Queens\\25Q032", "Queens\\25Q079",
"Queens\\25Q107", "Queens\\25Q120", "Queens\\25Q129", "Queens\\25Q130",
"Queens\\25Q154", "Queens\\25Q163", "Queens\\25Q164", "Queens\\25Q165",
"Queens\\25Q168", "Queens\\25Q169", "Queens\\25Q184", "Queens\\25Q185",
"Queens\\25Q189", "Queens\\25Q193", "Queens\\25Q194", "Queens\\25Q200",
"Queens\\25Q201", "Queens\\25Q209", "Queens\\25Q214", "Queens\\25Q219",
"Queens\\25Q237", "Queens\\25Q242", "Queens\\25Q244", "Queens\\25Q425",
"Queens\\25Q460", "Queens\\25Q499", "Queens\\25Q515", "Queens\\25Q707",
"Queens\\25Q792"), class = "factor"), Latitude = c(40.758365,
40.774168, 40.761748, 40.765602, 40.751762, 40.764834, 40.777951,
40.76501, 40.785959, 40.755764, 40.745593, 40.789929, 40.760072,
40.726437, 40.741093, 40.720926, 40.729642, 40.724191, 40.782346,
40.778164, 40.775122, 40.760344, 40.792326, 40.78322, 40.73106,
40.736521, 40.783714, 40.770194, 40.735855, 40.74943, 40.769753,
40.754465, 40.738775, 40.764868, 40.738332, 40.735017, 40.72535,
40.721466), Longitude = c(-73.824407, -73.818543, -73.811379,
-73.828293, -73.81778, -73.789712, -73.842932, -73.794319, -73.817349,
-73.799256, -73.829283, -73.839501, -73.783908, -73.807592, -73.808757,
-73.823358, -73.81781, -73.80937, -73.77844, -73.799841, -73.8185,
-73.817909, -73.809516, -73.806977, -73.805449, -73.813001, -73.795027,
-73.82762, -73.823583, -73.82141, -73.832001, -73.826204, -73.823892,
-73.826819, -73.82028, -73.821339, -73.811325, -73.820401), Windows.SEP.11 = c(48L,
154L, 11L, 62L, 20L, 72L, 9L, 37L, 8L, 22L, 9L, 47L, 44L, 99L,
78L, 91L, 42L, 122L, 55L, 14L, 162L, 108L, 89L, 87L, 23L, 14L,
75L, 74L, 141L, 73L, 43L, 14L, 534L, 189L, 128L, 10L, 79L, 38L
), Mac.SEP.11 = c(49L, 0L, 180L, 2L, 202L, 116L, 41L, 1L, 17L,
22L, 33L, 43L, 1L, 28L, 2L, 0L, 238L, 13L, 76L, 55L, 76L, 42L,
0L, 1L, 12L, 0L, 16L, 10L, 1L, 7L, 0L, 1L, 1L, 67L, 16L, 7L,
31L, 24L), Windows.SEP.12 = c(52L, 252L, 1L, 2L, 12L, 45L, 108L,
15L, 14L, 4L, 19L, 21L, 46L, 90L, 10L, 86L, 15L, 76L, 122L, 2L,
9L, 52L, 39L, 120L, 43L, 17L, 9L, 54L, 19L, 199L, 40L, 25L, 64L,
164L, 14L, 27L, 45L, 2L), Mac.SEP.12 = c(73L, 2L, 91L, 53L, 288L,
6L, 2L, 107L, 109L, 97L, 41L, 18L, 12L, 16L, 2L, 2L, 270L, 32L,
45L, 92L, 54L, 190L, 1L, 4L, 19L, 53L, 1L, 10L, 0L, 61L, 50L,
27L, 27L, 25L, 3L, 1L, 43L, 0L), newCol = c(56.3063063063063,
62.2549019607843, 32.5088339222615, 46.218487394958, 57.4712643678161,
21.3389121338912, 68.75, 76.25, 83.1081081081081, 69.6551724137931,
58.8235294117647, 30.2325581395349, 56.3106796116505, 45.4935622317597,
13.0434782608696, 49.1620111731844, 50.4424778761062, 44.4444444444444,
56.0402684563758, 57.6687116564417, 20.9302325581395, 61.734693877551,
31.0077519379845, 58.4905660377358, 63.9175257731959, 83.3333333333333,
9.9009900990099, 43.2432432432432, 11.8012422360248, 76.4705882352941,
67.6691729323308, 77.6119402985075, 14.5367412140575, 42.4719101123596,
10.5590062111801, 62.2222222222222, 44.4444444444444, 3.125)), .Names = c("School",
"Latitude", "Longitude", "Windows.SEP.11", "Mac.SEP.11", "Windows.SEP.12",
"Mac.SEP.12", "newCol"), row.names = c(NA, -38L), class = "data.frame")
You haven't provided us with any of the data, so I'm going to give an example using a dataset in the historydata package. Instead of getting a map based on a location and a zoom, you can get a map based on the bounding box of the latitudes and longitudes in your dataset.
library(historydata)
library(ggmap)
data("catholic_dioceses")
bbox <- make_bbox(catholic_dioceses$long, catholic_dioceses$lat, f = 0.01)
map <- get_map(bbox)
ggmap(map) +
geom_point(data=catholic_dioceses, aes(x = long, y = lat))
Note that the f = argument to make_bbox() lets you control how much padding there is around your map.
In your case, I think this will work:
library(ggmap)
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 0.01)
map <- get_map(bbox)
ggmap(map) +
geom_point(data=sep, aes(x = Longitude, y = Latitude,
color = Percent_SEP12_Assets),
size = 9, alpha = 0.6) +
scale_color_manual(breaks=c("Over 50", "Under 50"), values=c("green","red"))

Resources