crosstalk package error in datatable - r

When attempting to render a html app with crosstalk between leaflet/DT, I get the following error:
Error in datatable(sd, extensions = "Scroller", style = "bootstrap", class = "compact", : 'data' must be 2-dimensional (e.g. data frame or matrix)
data frame:
df2 <- data.frame(
structure(list(lat = c(-20.42, -20.62, -26, -17.97, -20.42, -19.68,
-11.7, -28.11, -28.74, -17.47, -21.44, -12.26, -18.54, -21, -20.7,
-15.94, -13.64, -17.83, -23.5, -22.63), long = c(181.62, 181.03,
184.1, 181.66, 181.96, 184.31, 166.1, 181.93, 181.74, 179.59,
180.69, 167, 182.11, 181.66, 169.92, 184.95, 165.96, 181.5, 179.78,
180.31), depth = c(562L, 650L, 42L, 626L, 649L, 195L, 82L, 194L,
211L, 622L, 583L, 249L, 554L, 600L, 139L, 306L, 50L, 590L, 570L,
598L), mag = c(4.8, 4.2, 5.4, 4.1, 4, 4, 4.8, 4.4, 4.7, 4.3,
4.4, 4.6, 4.4, 4.4, 6.1, 4.3, 6, 4.5, 4.4, 4.4), stations = c(41L,
15L, 43L, 19L, 11L, 12L, 43L, 15L, 35L, 19L, 13L, 16L, 19L, 10L,
94L, 11L, 83L, 21L, 13L, 18L)), .Names = c("lat", "long", "depth",
"mag", "stations"), row.names = c(NA, 20L), class = "data.frame")
)
Reproducible code:
library(crosstalk)
library(leaflet) #devtools::install_github('rstudio/leaflet', force = TRUE)
library(DT)
# Wrap data frame in SharedData
sd <- SharedData$new(quakes[sample(nrow(quakes), 10),])
# Create a filter input
filter_slider("mag", "Magnitude", sd, column=~mag, step=0.1, width=250)
# Use SharedData like a dataframe with Crosstalk-enabled widgets
bscols(
leaflet(sd) %>% addTiles() %>% addMarkers(),
datatable(sd, extensions="Scroller", style="bootstrap", class="compact", width="100%",
options=list(deferRender=TRUE, scrollY=300, scroller=FALSE))
)
And the platform and pkg versions:
R version 3.3.2 (2016-10-31)
crosstalk_1.0.1 (#installed from devtools/github)
leaflet_1.0.2.9010
DT_0.2

install 'DT' from devtool/github.
devtools::install_github('rstudio/DT')

Related

Fuzzy Join Error: All columns in a tibble must be vectors

test <- structure(list(trip_count = 1:10, dropoff_longitude = c(-73.959862,
-73.882202, -73.934113, -73.992203, -74.00563, -73.975189, -73.97448,
-73.974838, -73.981377, -73.955093), dropoff_latitude = c(40.773617,
40.744175, 40.715923, 40.749203, 40.726158, 40.729824, 40.763599,
40.754135, 40.759987, 40.765224)), row.names = c(NA, -10L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x7fd18800f6e0>)
> dput(zip_codes)
zip_codes <- structure(list(zipcode = c("10001", "10002", "10003", "10004",
"10005", "10006", "10007", "10009", "10010", "10011", "10012",
"10013", "10014", "10016", "10017", "10018", "10019", "10020",
"10021", "10022", "10023", "10024", "10025", "10026", "10027",
"10028", "10029", "10030", "10031", "10032", "10033", "10034",
"10035", "10036", "10037", "10038", "10039", "10040", "10044",
"10065", "10069", "10075", "10103", "10110", "10111", "10112",
"10115", "10119", "10128", "10152", "10153", "10154", "10162",
"10165", "10167", "10168", "10169", "10170", "10171", "10172",
"10173", "10174", "10177", "10199", "10271", "10278", "10279",
"10280", "10282"), bounds_north = c(40.759731, 40.724136, 40.739673,
40.709044, 40.709294, 40.71369, 40.71719, 40.734975, 40.745421,
40.756703, 40.731706, 40.727557, 40.742873, 40.752197, 40.757912,
40.762526, 40.773446, 40.761094, 40.775045, 40.764898, 40.783192,
40.818099, 40.811264, 40.807546, 40.822108, 40.782213, 40.800665,
40.824032, 40.834372, 40.850517, 40.861552, 40.87765, 40.809582,
40.765558, 40.819569, 40.714451, 40.846615, 40.866336, 40.772955,
40.770517, 40.781007, 40.777677, 40.761771, 40.755516, 40.759689,
40.759899, 40.811331, 40.751522, 40.787914, 40.759059, 40.764279,
40.758432, 40.770085, 40.752801, 40.755303, 40.752119, 40.754974,
40.753811, 40.756556, 40.755928, 40.754783, 40.752116, 40.7556,
40.752723, 40.708797, 40.71628, 40.713256, 40.714767, 40.719611
), bounds_south = c(40.743451, 40.708802, 40.722933, 40.683919,
40.702879, 40.705871, 40.709806, 40.718612, 40.73231, 40.731043,
40.719867, 40.713446, 40.72428, 40.73801, 40.747251, 40.749102,
40.758645, 40.757284, 40.758133, 40.751445, 40.768436, 40.778805,
40.788476, 40.79691, 40.803047, 40.770062, 40.782531, 40.812791,
40.817221, 40.829083, 40.842958, 40.849745, 40.781075, 40.752197,
40.806636, 40.701689, 40.817912, 40.851863, 40.749415, 40.759284,
40.771612, 40.769441, 40.759787, 40.753481, 40.758538, 40.758436,
40.810373, 40.749101, 40.773108, 40.757749, 40.762964, 40.757125,
40.768355, 40.75146, 40.753994, 40.750775, 40.753811, 40.751441,
40.755243, 40.754619, 40.753481, 40.750766, 40.754678, 40.750241,
40.707694, 40.714082, 40.711995, 40.700273, 40.713378), bounds_east = c(-73.984076,
-73.973635, -73.979864, -73.995657, -74.004569, -74.009988, -74.000455,
-73.971282, -73.971566, -73.990798, -73.991794, -73.994035, -73.999555,
-73.968192, -73.964271, -73.981822, -73.973015, -73.977201, -73.947973,
-73.958599, -73.974067, -73.960687, -73.954966, -73.944667, -73.940404,
-73.944337, -73.930891, -73.936232, -73.938588, -73.934671, -73.92216,
-73.910587, -73.914228, -73.978116, -73.933219, -73.991772, -73.929107,
-73.924385, -73.940026, -73.952085, -73.986609, -73.947039, -73.975831,
-73.980395, -73.976744, -73.97845, -73.963058, -73.99111, -73.937328,
-73.970993, -73.971411, -73.971451, -73.94827, -73.977677, -73.973735,
-73.976048, -73.975209, -73.974648, -73.97282, -73.973276, -73.978332,
-73.973959, -73.975352, -73.993948, -74.009829, -74.002115, -74.007666,
-74.013754, -74.012441), bounds_west = c(-74.008621, -73.997532,
-73.999604, -74.047285, -74.012508, -74.015905, -74.013754, -73.988643,
-73.994028, -74.012359, -74.004575, -74.016381, -74.01599, -73.987746,
-73.981822, -74.007989, -74.003477, -73.98373, -73.968441, -73.977655,
-73.990149, -73.98814, -73.977092, -73.962475, -73.9659, -73.96323,
-73.955778, -73.948677, -73.960007, -73.950403, -73.944672, -73.947051,
-73.946462, -74.001702, -73.943398, -74.010542, -73.943506, -73.938947,
-73.961583, -73.972553, -73.996142, -73.965148, -73.979513, -73.984118,
-73.97845, -73.980886, -73.964424, -73.994844, -73.959921, -73.973068,
-73.973465, -73.973524, -73.951858, -73.979768, -73.975807, -73.978159,
-73.976974, -73.977107, -73.974897, -73.975352, -73.980395, -73.976048,
-73.976516, -74.00143, -74.011248, -74.00542, -74.009668, -74.019603,
-74.01831), zip = c(10001, 10002, 10003, 10004, 10005, 10006,
10007, 10009, 10010, 10011, 10012, 10013, 10014, 10016, 10017,
10018, 10019, 10020, 10021, 10022, 10023, 10024, 10025, 10026,
10027, 10028, 10029, 10030, 10031, 10032, 10033, 10034, 10035,
10036, 10037, 10038, 10039, 10040, 10044, 10065, 10069, 10075,
10103, 10110, 10111, 10112, 10115, 10119, 10128, 10152, 10153,
10154, 10162, 10165, 10167, 10168, 10169, 10170, 10171, 10172,
10173, 10174, 10177, 10199, 10271, 10278, 10279, 10280, 10282
)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 10L, 11L, 12L,
13L, 14L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L,
40L, 43L, 50L, 51L, 53L, 67L, 74L, 75L, 76L, 79L, 83L, 91L, 101L,
102L, 103L, 111L, 114L, 116L, 117L, 118L, 119L, 120L, 121L, 122L,
123L, 126L, 133L, 151L, 158L, 159L, 160L, 162L), class = "data.frame")
Hey guys, so I am trying to fuzzy-join lat & lon information to get the zip code of a specific location. I tried:
test <- test %>% fuzzy_left_join(zip_codes,by = c("dropoff_longitude" = "bounds_east", "dropoff_longitude" = "bounds_west", "dropoff_latitude" = "bounds_north","dropoff_latitude" = "bounds_south"), match_fun = list('<=', '>=' , '<=', '>='))
But unfortunately, this returns the error message Error: All columns in a tibble must be vectors. x Column "col" is NULL.
I don't know how to solve this. There is no column "col" in either one of the data frames. The result should give me the correspondent zip code if the dropoff_longitute is between bounds_east and bounds_west and the dropoff_latitude is between bounds_north and bounds_south.
Thanks a lot in advance!
We could use the non-equi join from data.table as one of the dataset is data.table
library(data.table)
setDT(test)[zip_codes, on = .(dropoff_longitude <= bounds_east,
dropoff_longitude >= bounds_west,
dropoff_latitude <= bounds_north,
dropoff_latitude >= bounds_south)]

Show all datapoints while specifying axis labels in ggplot in R

I am creating a scatterplot using ggplot. I am able to create a scatterplot using the following code.
ggplot(df2, aes(x = date, y = mean, color = NULL)) +
geom_point(position = "jitter") +
labs(title = "ShotSpotter incidents around July 4th",
x = "Day of year", y = "Mean daily gunshots") +
labs(fill = "Treatment Status") +
geom_segment(aes(x = "07-01", xend = "07-01", y = 0, yend = 50), colour = "red")
I would like to change the labels on the x-axis so that they are easier to read. When I try to do so using scale_x_discrete(), most of the datapoints disappear except for those corresponding to the values now labeled on the x-axis.
ggplot(df2, aes(x = date, y = mean, color = NULL)) +
geom_point(position = "jitter") +
labs(title = "ShotSpotter incidents around July 4th",
x = "Day of year", y = "Mean daily gunshots") +
labs(fill = "Treatment Status") +
geom_segment(aes(x = "07-01", xend = "07-01", y = 0, yend = 50), colour = "red") +
scale_x_discrete(limits = c("05-01", "06-01", "07-01", "08-01", "09-01"),
labels = c("May 1", "June 1", "July 1", "Aug 1", "Sept 1"))
How can I keep the labels from the 2nd graph and include all the datapoints shown in the 1st?
Data using dput():
structure(list(date = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L,
34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L,
60L, 61L, 62L, 63L, 64L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L,
76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L,
89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L,
101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L,
112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L,
123L, 124L), .Label = c("05-01", "05-02", "05-03", "05-04", "05-05",
"05-06", "05-07", "05-08", "05-09", "05-10", "05-11", "05-12",
"05-13", "05-14", "05-15", "05-16", "05-17", "05-18", "05-19",
"05-20", "05-21", "05-22", "05-23", "05-24", "05-25", "05-26",
"05-27", "05-28", "05-29", "05-30", "05-31", "06-01", "06-02",
"06-03", "06-04", "06-05", "06-06", "06-07", "06-08", "06-09",
"06-10", "06-11", "06-12", "06-13", "06-14", "06-15", "06-16",
"06-17", "06-18", "06-19", "06-20", "06-21", "06-22", "06-23",
"06-24", "06-25", "06-26", "06-27", "06-28", "06-29", "06-30",
"07-01", "07-02", "07-03", "07-04", "07-05", "07-06", "07-07",
"07-08", "07-09", "07-10", "07-11", "07-12", "07-13", "07-14",
"07-15", "07-16", "07-17", "07-18", "07-19", "07-20", "07-21",
"07-22", "07-23", "07-24", "07-25", "07-26", "07-27", "07-28",
"07-29", "07-30", "07-31", "08-01", "08-02", "08-03", "08-04",
"08-05", "08-06", "08-07", "08-08", "08-09", "08-10", "08-11",
"08-12", "08-13", "08-14", "08-15", "08-16", "08-17", "08-18",
"08-19", "08-20", "08-21", "08-22", "08-23", "08-24", "08-25",
"08-26", "08-27", "08-28", "08-29", "08-30", "08-31", "09-01"
), class = "factor"), mean = c(13, 15, 16.5, 17.6666666666667,
14.5, 13.3333333333333, 11.8333333333333, 13, 13, 14.3333333333333,
13.8333333333333, 15.5, 11.1666666666667, 15, 12.5, 15.6666666666667,
14.5, 10.5, 11.6666666666667, 17.5, 14.5, 13, 14.6666666666667,
15.6666666666667, 21.3333333333333, 30.6666666666667, 18.5, 17.5,
13.5, 18.5, 13.3333333333333, 14.5, 14.8333333333333, 9.66666666666667,
15.8333333333333, 13.5, 20.5, 16.1666666666667, 15.1666666666667,
14.8333333333333, 15.3333333333333, 14.1666666666667, 14.5, 13.6666666666667,
20.1666666666667, 17.8333333333333, 22.3333333333333, 15.8333333333333,
15.5, 16.1666666666667, 15, 20, 20.8333333333333, 20.8333333333333,
25, 21.1666666666667, 18.1666666666667, 27, 19.5, 19.5, 19.6666666666667,
25.6666666666667, 36.8333333333333, 46.6666666666667, 40.5, 21.3333333333333,
16.3333333333333, 18, 20.1666666666667, 22.6666666666667, 16.8333333333333,
13.8333333333333, 14.5, 14.1666666666667, 16.5, 15.1666666666667,
15.1666666666667, 13.3333333333333, 13.3333333333333, 12.6666666666667,
12.8333333333333, 12.3333333333333, 16.5, 19.6666666666667, 16.3333333333333,
10.6666666666667, 13.1666666666667, 17.5, 10.3333333333333, 15.5,
12.1666666666667, 14.3333333333333, 13.8333333333333, 11.6666666666667,
13, 10.6666666666667, 17.5, 19.3333333333333, 12.6666666666667,
12.5, 12.5, 13.5, 15.8333333333333, 13.5, 15.6666666666667, 16.3333333333333,
14.5, 13.8333333333333, 14.3333333333333, 11, 13.3333333333333,
23.8333333333333, 14.1666666666667, 13.5, 13.3333333333333, 13.3333333333333,
14, 10.6666666666667, 14.3333333333333, 13.3333333333333, 13.1666666666667
)), class = "data.frame", row.names = c(NA, -121L))
df2$date <- as.Date(paste0("2000-", as.character(df2$date)))
ggplot(df2, aes(x = date, y = mean, color = NULL)) +
geom_point(position = "jitter") +
labs(title = "ShotSpotter incidents around July 4th",
x = "Day of year", y = "Mean daily gunshots") +
labs(fill = "Treatment Status") +
geom_segment(aes(x = as.Date("2000-07-01"), xend = as.Date("2000-07-01"), y = 0, yend = 50), colour = "red") +
scale_x_date(labels = scales::date_format("%b-%d"))
This required the two changes: as.Date (I'm supposing 2000 year just for something), and changing geom_segment so that x and xend are Date objects.
We can use
library(dplyr)
library(lubridate)
library(ggplot2)
df2 %>%
mutate(date = mdy(date, truncated = 2)) %>%
ggplot(aes(x = date, y = mean, color = NULL)) +
geom_point(position = "jitter") +
labs(title = "ShotSpotter incidents around July 4th",
x = "Day of year", y = "Mean daily gunshots") +
labs(fill = "Treatment Status") +
geom_segment(aes(x = mdy('07-01', truncated = 2),
xend = mdy('07-01', truncated = 2), y=0, yend = 50), colour = 'red') +
scale_x_date(labels = scales::date_format("%b-%d"))
-output

Plotting results with missing categories in interaction with emmeans

I have a quite "messy data". I have a model with a interaction between two factors. And I want to plot it. So:
f1 <- structure(list(tipo = c("digitables", "digitables", "digitables",
"digitables", "digitables", "digitables", "digitables", "digitables",
"payments", "payments", "payments", "payments", "payments", "payments",
"payments", "payments", "traditionals", "traditionals", "traditionals",
"traditionals", "traditionals", "traditionals", "traditionals",
"traditionals"), categoria = c("Advice", "Digital banks", "Exchange",
"FinTech", "Insurance", "Investments", "Lending", "Payments and transfers",
"Advice", "Digital banks", "Exchange", "FinTech", "Insurance",
"Investments", "Lending", "Payments and transfers", "Advice",
"Digital banks", "Exchange", "FinTech", "Insurance", "Investments",
"Lending", "Payments and transfers"), Total = c(63L, 450L, 279L,
63L, 36L, 108L, 567L, 549L, 63L, 450L, 279L, 63L, 36L, 108L,
567L, 549L, 35L, 250L, 155L, 35L, 20L, 60L, 315L, 305L), Frequencia = c(44L,
266L, 118L, 9L, 14L, 45L, 134L, 242L, 33L, 68L, 2L, 10L, 3L,
8L, 11L, 78L, 27L, 226L, 142L, 10L, 20L, 45L, 300L, 245L), Perc = c(69.84,
59.11, 42.29, 14.29, 38.89, 41.67, 23.63, 44.08, 52.38, 15.11,
0.72, 15.87, 8.33, 7.41, 1.94, 14.21, 77.14, 90.4, 91.61, 28.57,
100, 75, 95.24, 80.33), Failure = c(19L, 184L, 161L, 54L, 22L,
63L, 433L, 307L, 30L, 382L, 277L, 53L, 33L, 100L, 556L, 471L,
8L, 24L, 13L, 25L, 0L, 15L, 15L, 60L)), row.names = c(NA, -24L
), class = "data.frame")
# Packages
library(dplyr)
library(ggplot2)
library(emmeans) #version 1.4.8. or 1.5.1
# Works as expected
m1 <- glm(cbind(Frequencia, Failure) ~ tipo*categoria,
data = f1, family = binomial(link = "logit"))
l1 <- emmeans(m1, ~categoria|tipo)
plot(l1, type = "response",
comparison = T,
by = "categoria")
Using by="tipo" results:
# Doesn't work:
plot(l1, type = "response",
comparison = T,
by = "tipo")
Error: Aborted -- Some comparison arrows have negative length!
In addition: Warning message:
Comparison discrepancy in group digitables, Advice - Insurance:
Target overlap = -0.0241, overlap on graph = 0.0073
If I use comparison = F as suggested by explanation supplement vignette, it works. However, it does not show me the arrows, which are very important.
Q1 - Is there a work around for it? (Or is it impossible due to my data?)
As we can see from the last plot, there is a category with probability = 1 (categoria=Insurance and tipo=traditionals). So, I delete only this row of my data frame, and I try to redo the plotting, and results to me:
f1 <- f1 %>%
filter(!Perc ==100)
m1 <- glm(cbind(Frequencia, Failure) ~ tipo*categoria,
data = f1, family = binomial(link = "logit"))
l1 <- emmeans(m1, ~categoria|tipo)
plot(l1, type = "response",
comparison = T,
by = "categoria")
Error in if (dif[i] > 0) lmat[i, id1[i]] = rmat[i, id2[i]] = wgt * v1[i] else rmat[i, :
missing value where TRUE/FALSE needed
Q2 - How to plot my results even when I have a missing level of one variable (with respect to another variable?). I would expect that the Insurance facet would have only have the payments and digitables levels (while the others remain the same).
First, please don't ever re-use the same variable names for more than one thing; that makes things not reproducible. If you modify a dataset, or a model, or whatever, give it a new name so it can be distinguished.
Q1
As documented, comparison arrows cannot always be computed. This is such an example. I suggest displaying the results some other way, e.g. using pwpp() or pwpm()
Q2
There was a bug in handling missing cases. This has been fixed in the GitHub version:
f2 <- f1 %>%
filter(!Perc ==100)
m2 <- glm(cbind(Frequencia, Failure) ~ tipo*categoria,
data = f2, family = binomial(link = "logit"))
l2 <- emmeans(m2, ~categoria|tipo)
plot(l2, type = "response",
comparison = TRUE,
by = "categoria")
plot(l2, type = "response",
comparison = TRUE,
by = "tipo")
## Error: Aborted -- Some comparison arrows have negative length!
## (in group "payments")

Adding axis labels and title to ggballoonplot()

The code I used and the result can be seen in the image below. The main problem is that the title doesn't appear in the center and the x and y labels don't appear at all. How do I fix this?
The graph and code
You should upload your code as a snippet and your data so we can reproduce this on our own machines easily...
Take the example below. You can recreate the data set and then run the code immediately.
Using ggtitle, xlab, ylab you can plot the text and center it with theme.
If this does not help you have the wrong print / render settings.
balloon <- data.table(structure(list(Genera = c("Prevotella", "Treponema", "Fusobacterium","Selenomonas", "Veillonella", "Porphyromonas", "Streptococcus","Leptotrichia", "Aggregatibacter", "Succiniclasticum"), S1 = c(97L,28L, 11L, 40L, 5L, 13L, 10L, 24L, 0L, 16L), S3 = c(5370L, 3760L,5551L, 2087L, 533L, 873L, 1330L, 5877L, 1213L, 44L), S4 = c(7892L,8004L, 11017L, 19712L, 5115L, 2695L, 7451L, 13611L, 301L, 2557L), S5 = c(23L, 79L, 30L, 7L, 0L, 34L, 0L, 2L, 2L, 0L), S6 = c(8310L,3379L, 38058L, 1133L, 2506L, 17811L, 12103L, 403L, 668L, 3L),S2 = c(7379L, 14662L, 10085L, 148L, 1502L, 5222L, 1010L,2463L, 4790L, 28L), S7 = c(6238L, 18977L, 2674L, 2198L, 27L,2999L, 174L, 1197L, 5268L, 5L), S8 = c(20019L, 18674L, 15306L,1472L, 1898L, 9600L, 1683L, 2221L, 3435L, 1109L), S9 = c(153L,12L, 23L, 36L, 15L, 15L, 6L, 41L, 0L, 30L), S10 = c(20103L,29234L, 10857L, 2869L, 4923L, 14206L, 1415L, 4574L, 649L,2160L)), .Names = c("Genera", "S1", "S3", "S4", "S5", "S6","S2", "S7", "S8", "S9", "S10"), class = c("data.table", "data.frame"), row.names = c(NA, -10L)))
library(ggplot2)
library(reshape2)
library(data.table)
balloon<-fread("Downloads/balloon.csv")
balloon
balloon_melted<-melt(balloon)
head(balloon_melted)
p <- ggplot(balloon_melted, aes(x =variable, y = Genera))
p+
geom_point( aes(size=value))+
theme(panel.background=element_blank(),
panel.border = element_rect(colour = "blue", fill=NA, size=1)) +
ggtitle("Pretty title") +
xlab("x lab label") +
ylab("y lab label") +
theme(plot.title = element_text(hjust = 0.5))

plot graphs in R while showing all the x axis values

I'm trying to plot a graph with my data.
My code for that is
plot(birthRate$country_code, birthRate$yr2014, main = "Birth Rate by Countries 2014")
My out put is like this:
But I want to show all values in x axis.
dput(birthRate):
structure(list(series_code = structure(c(21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L), .Label = c("NY.GNP.PCAP.CD",
"SE.PRM.ENRR", "SE.SEC.ENRR", "SE.TER.ENRR", "SE.TER.ENRR.FE",
"SH.ALC.PCAP.LI", "SH.DTH.COMM.ZS", "SH.DTH.INJR.ZS", "SH.DTH.NCOM.ZS",
"SH.IMM.IBCG", "SH.STA.MMRT.NE", "SH.STA.TRAF.P5", "SH.XPD.PCAP",
"SH.XPD.PRIV.ZS", "SH.XPD.PUBL.ZS", "SH.XPD.TOTL.ZS", "SL.UEM.TOTL.FE.ZS",
"SL.UEM.TOTL.MA.ZS", "SL.UEM.TOTL.ZS", "SP.ADO.TFRT", "SP.DYN.CBRT.IN",
"SP.DYN.CDRT.IN", "SP.DYN.LE00.FE.IN", "SP.DYN.LE00.IN",
"SP.DYN.LE00.MA.IN",
"SP.DYN.TFRT.IN"), class = "factor"), country_name = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 14L, 15L, 17L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 28L, 29L), .Label = c("Australia",
"Brunei Darussalam", "Cambodia", "China", "Fiji", "Indonesia",
"Japan", "Kiribati", "Korea, Dem. People’s Rep.", "Korea, Rep.",
"Lao PDR", "Malaysia", "Marshall Islands", "Micronesia, Fed. Sts.",
"Mongolia", "Nauru", "New Zealand", "Palau", "Papua New Guinea",
"Philippines", "Samoa", "Singapore", "Solomon Islands", "Thailand",
"Timor-Leste", "Tonga", "Tuvalu", "Vanuatu", "Vietnam"), class = "factor"),
country_code = structure(c(1L, 2L, 8L, 3L, 4L, 6L, 7L, 9L,
20L, 10L, 11L, 14L, 5L, 13L, 16L, 19L, 17L, 29L, 21L, 22L,
23L, 24L, 25L, 28L, 27L), .Label = c("AUS", "BRN", "CHN",
"FJI", "FSM", "IDN", "JPN", "KHM", "KIR", "KOR", "LAO", "MHL",
"MNG", "MYS", "NRU", "NZL", "PHL", "PLW", "PNG", "PRK", "SGP",
"SLB", "THA", "TLS", "TON", "TUV", "VNM", "VUT", "WSM"), class = "factor"),
yr2001 = c(12.7, 20.913, 27.327, 13.38, 24.41, 21.486, 9.3,
30.228, 17.414, 11.6, 30.999, 21.445, 29.21, 19.035, 14.36,
34.396, 29.301, 30.269, 11.8, 35.403, 14.025, 41.441, 28.365,
31.84, 17.13), yr2002 = c(12.8, 20.137, 26.793, 12.86, 24.103,
21.49, 9.3, 29.965, 16.92, 10.2, 30.287, 20.39, 28.453, 19.001,
13.67, 33.95, 28.892, 29.991, 11.4, 35.226, 13.653, 40.428,
28.468, 31.219, 16.921), yr2003 = c(12.6, 19.522, 26.44,
12.41, 23.804, 21.5, 9.2, 29.775, 16.431, 10.2, 29.753, 19.435,
27.669, 19.209, 13.94, 33.49, 28.404, 29.778, 10.5, 35.061,
13.32, 39.726, 28.565, 30.597, 16.839), yr2004 = c(12.3,
19.065, 26.24, 12.29, 23.508, 21.499, 8.6936, 29.647, 15.961,
9.8, 29.38, 18.62, 26.886, 19.627, 14.2, 33.03, 27.845, 29.624,
10.3, 34.889, 13.025, 39.368, 28.624, 29.993, 16.848), yr2005 = c(12.8,
18.738, 26.145, 12.4, 23.208, 21.476, 8.4133, 29.572, 15.532,
8.9, 29.134, 17.971, 26.139, 20.223, 13.96, 32.575, 27.238,
29.499, 10.2, 34.68, 12.764, 39.326, 28.611, 29.427, 16.919
), yr2006 = c(12.9, 18.499, 26.098, 12.09, 22.901, 21.429,
8.65, 29.537, 15.166, 9.2, 28.966, 17.498, 25.461, 20.959,
14.14, 32.121, 26.619, 29.355, 10.3, 34.409, 12.533, 39.509,
28.499, 28.92, 17.03), yr2007 = c(14.1, 18.292, 26.043, 12.1,
22.586, 21.364, 8.63, 29.528, 14.87, 10, 28.821, 17.171,
24.872, 21.769, 15.15, 31.659, 26.025, 29.148, 10, 34.063,
12.323, 39.752, 28.288, 28.475, 17.163), yr2008 = c(14, 18.07,
25.937, 12.14, 22.263, 21.283, 8.7, 29.526, 14.648, 9.4,
28.651, 16.954, 24.385, 22.576, 15.1, 31.186, 25.489, 28.845,
10.2, 33.637, 12.123, 39.92, 27.982, 28.091, 17.298), yr2009 = c(13.9,
17.809, 25.755, 12.13, 21.929, 21.177, 8.5, 29.513, 14.498,
9, 28.429, 16.828, 24.011, 23.311, 14.53, 30.706, 25.023,
28.442, 9.9, 33.132, 11.927, 39.95, 27.588, 27.766, 17.409
), yr2010 = c(13.7, 17.499, 25.491, 11.9, 21.583, 21.034,
8.5, 29.468, 14.411, 9.4, 28.142, 16.773, 23.751, 23.892,
14.68, 30.229, 24.634, 27.944, 9.3, 32.555, 11.725, 39.8,
27.112, 27.486, 17.473), yr2011 = c(13.6, 17.146, 25.164,
11.93, 21.221, 20.841, 8.3, 29.377, 14.374, 9.4, 27.8, 16.765,
23.598, 24.252, 14, 29.764, 24.315, 27.372, 9.5, 31.918,
11.51, 39.461, 26.57, 27.236, 17.477), yr2012 = c(13.7, 16.774,
24.812, 12.1, 20.846, 20.595, 8.2, 29.235, 14.363, 9.6, 27.43,
16.783, 23.528, 24.378, 13.87, 29.318, 24.041, 26.768, 10.1,
31.25, 11.281, 38.985, 25.992, 26.993, 17.424), yr2013 = c(13.3,
16.405, 24.462, 12.08, 20.463, 20.297, 8.2, 29.044, 14.358,
8.6, 27.051, 16.805, 23.511, 24.275, 13.2, 28.899, 23.79,
26.172, 9.3, 30.578, 11.041, 38.419, 25.409, 26.739, 17.318
), yr2014 = c(12.9, 16.043, 24.119, 12.4, 20.075, 19.955,
8, 28.8, 14.349, 8.6, 26.666, 16.811, 23.531, 23.949, 12.68,
28.51, 23.552, 25.608, 9.8, 29.921, 10.79, 37.783, 24.846,
26.466, 17.157)), .Names = c("series_code", "country_name",
"country_code", "yr2001", "yr2002", "yr2003", "yr2004", "yr2005",
"yr2006", "yr2007", "yr2008", "yr2009", "yr2010", "yr2011", "yr2012",
"yr2013", "yr2014"), row.names = c(30L, 31L, 32L, 33L, 34L, 35L,
36L, 37L, 38L, 39L, 40L, 41L, 43L, 44L, 46L, 48L, 49L, 50L, 51L,
52L, 53L, 54L, 55L, 57L, 58L), class = "data.frame", na.action =
structure(c(13L, 16L, 18L, 27L), .Names = c("42", "45", "47", "56"), class = "omit"))
You could try plotting the x-axis labels horizontally.
Try this out:
plot(birthRate$country_code, birthRate$yr2014, main = "Birth Rate by Countries 2014", las=2)
Edit:
I was able to make this barplot using ggplot2.
Here's my code:
birthRate <- arrange(birthRate, yr2014)
p <- ggplot(birthRate, aes(y=yr2014, x=reorder(country_code, yr2014), fill=country_code)) +
geom_col()
p
Note: Your dput() output has 29 observations for the country_code variable, but only 25 for the yr2014 variable. I didn't know exactly where the missing data was, so I just removed the last four observations from the country_code variable to get things to line up. Your output may look slightly different based on where the NAs are...
I hope this was helpful!

Resources