Change the column width on a tableGrob using ttheme_minimal - r

I'm trying to build a table using tableGrob in gridExtra and can't figure out how to set the column with. I am using ttheme_minimal(base_size = 25) to set the size of the text, but I can't seem to find a way in that function to se the column widths. Below is the full code that I'm using:
team_free_agents <- structure(list(Player = c("Adrian Amos", "Dean Lowry", "Marcedes Lewis",
"Allen Lazard", "Robert Tonyan", "Jarran Reed", "Randall Cobb",
"Rudy Ford", "Yosh Nijman", "Keisean Nixon", "Justin Hollins"
), Position = c("S", "IDL", "TE", "WR", "TE", "IDL", "WR", "S",
"LT", "CB", "EDGE"), Age = c(30, 29, 39, 28, 29, 31, 33, 29,
27, 26, 27), `Snap %` = c("94.6%", "46.7%", "41.2%", "78.9%",
"54%", "68.2%", "33.9%", "42.8%", "69.1%", "28%", "40.7%"), `Current APY` = c("$9,000,000",
"$6,775,000", "$4,000,000", "$3,986,000", "$3,750,000", "$3,250,000",
"$3,000,000", "$1,137,500", "$965,000", "$965,000", "$706,724"
), `Current Guarantees` = c("$12,000,000", "$6,000,000", "$2,100,000",
"$0", "$1,000,000", "$1,865,000", "$0", "$0", "$0", "$0", "$306,896"
), `2022 PFF Grade` = c(54.2, 59.3, 65.6, 69, 57.7, 61.9, 70.1,
77.7, 63.1, 63.9, 54)), row.names = c(NA, -11L), class = c("tbl_df",
"tbl", "data.frame"))
tt1 <- gridExtra::ttheme_minimal(base_size = 25)
fa_table <- gridExtra::tableGrob(team_free_agents,
rows = NULL,
theme = tt1)
cowplot::draw_grob(
fa_table,
width = 90
)
Any help with this would be appreciated!

Related

Only the map legend appearing in ggplot

I am trying to plot data on a map. However, only the legend appears on a blank graph. The data set.
structure(list(States.Uts = c("Jammu and Kashmir", "Karnataka",
"Odisha", "Sikkim", "Madhya Pradesh", "Maharashtra", "Kerala",
"Rajasthan", "Delhi", "Andhra Pradesh", "Uttar Pradesh", "Gujarat",
"West Bengal", "Mizoram", "Uttarakhand", "Assam", "Haryana",
"Himachal Pradesh", "Jharkhand", "Punjab"), id = c(35, 8, 36,
25, 11, 12, 9, 21, 5, 17, 24, 22, 1, 30, 23, 26, 6, 19, 7, 20
), long = c(74.8692906760123, 76.1671602023197, 84.4299347735266,
88.47355094813, 78.2889834734203, 76.1073683433136, 76.4080579710788,
73.8499033398323, 77.1154800785184, 79.964340328073, 80.5663333555389,
71.5737853588608, 87.9835046818375, 92.8318089515678, 79.2071970183086,
92.8261810120493, 76.3401988564537, 77.2453583452855, 85.5641242711505,
75.4154856775423), lat = c(33.7066861126216, 14.7103409600977,
20.5129916361452, 27.5703696825569, 23.5382000019923, 19.4517685463546,
10.4515827401328, 26.5845654103506, 28.6433836730517, 15.7549664734525,
26.9232961978376, 22.6974841269408, 23.8143407874487, 23.3070171585463,
30.1564981917953, 26.3553441447585, 29.198093425323, 31.92360060294,
23.6561315040147, 30.8424285351448), Type = c("Union Territory",
"State", "State", "State", "State", "State", "State", "State",
"Union Territory", "State", "State", "State", "State", "State",
"State", "State", "State", "State", "State", "State"), low_prestige = c(1000,
836, 195, 1000, 188, 441, 441, 736, 370, 235, 0, 151, 82, 42,
28, 0, 0, 0, 0, 0), high_prestige = c(0, 0, 0, 0, 53, 149, 212,
264, 630, 765, 808, 849, 918, 958, 972, 1000, 1000, 1000, 1000,
1000)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-20L))
The code that I have tried:
map1<-ggplot(final.plot1, aes( x = long, y = lat, group=id)) +
geom_polygon(aes(fill = low_prestige), color = "black")
map1
Another option is using the sf package which is useful to plot geographic data in combination with ggplot. Here is a reproducible example:
library(ggplot2)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
your_sf <- st_as_sf(final.plot1, coords = c('long', 'lat'))
ggplot(your_sf) +
geom_sf(aes(color = low_prestige))
Created on 2022-11-28 with reprex v2.0.2

use of pivot_wider to plot the evolution of variables in R

I would like to plot the evolution of the number of workers per category ("A", "D", "F", "I"), from 2017 to 2021, with a stacked bar chart (with the labels in the middle of each bar, for each category), one bar per year. Yet my dataset isn't in the right way to do this, I think I need to use pivot_wider() or pivot_longer() from what I have seen here, but I don't really know how to manipulate these functions. Could anyone help ?
Here is the structure of my dataset, for reproducibility :
structure(list(A = c("10", "7", "8", "8", "9", "Total"), D = c(23,
14, 29, 35, 16, 117), F = c(8, 7, 11, 6, 6, 38), I = c(449, 498,
415, 470, 531, 2363), annee = c("2017", "2018", "2019", "2020",
"2021", NA)), core = structure(list(A = c("10", "7", "8", "8",
"9"), D = c(23, 14, 29, 35, 16), F = c(8, 7, 11, 6, 6), I = c(449,
498, 415, 470, 531)), class = "data.frame", row.names = c(NA,
-5L)), tabyl_type = "two_way", totals = "row", row.names = c(NA,
6L), class = c("tabyl", "data.frame"))
library(tidyverse)
library(ggrepel)
df <- structure(list(A = c("10", "7", "8", "8", "9", "Total"), D = c(
23,
14, 29, 35, 16, 117
), F = c(8, 7, 11, 6, 6, 38), I = c(
449, 498,
415, 470, 531, 2363
), annee = c(
"2017", "2018", "2019", "2020",
"2021", NA
)), core = structure(list(A = c(
"10", "7", "8", "8",
"9"
), D = c(23, 14, 29, 35, 16), F = c(8, 7, 11, 6, 6), I = c(
449,
498, 415, 470, 531
)), class = "data.frame", row.names = c(
NA,
-5L
)), tabyl_type = "two_way", totals = "row", row.names = c(
NA,
6L
), class = c("tabyl", "data.frame"))
df |>
filter(!is.na(annee)) |>
mutate(A = as.double(A)) |>
pivot_longer(-annee, names_to = "category") |>
ggplot(aes(annee, value, fill = category, label = value)) +
geom_col() +
geom_label_repel(position = position_stack(), max.overlaps = 20)
Created on 2022-08-08 by the reprex package (v2.0.1)
Once you remove the total row, and ensuring that A through I are numeric, you can pivot_longer and pass to ggplot() like this:
data %>%
filter(A!="Total") %>%
mutate(across(A:I, as.numeric)) %>%
pivot_longer(cols = -annee, names_to = "group", values_to = "ct") %>%
ggplot(aes(annee,ct,fill=group)) +
geom_col()
I did not add the category labels, since group I dominates each year; you might want to reconsider that visualization

Error: Join columns must be present in data error, but columns are separated, tidy, and still having problem

I'm having issues joining a set of columns with a simple inner_join even though all of my data is tidy. Below is the error that I receive and below that I will paste simple samples of my data.
library(tidyverse)
library(janitor)
regions_name = regions %>% select(region, name)
regions_name$region = as.numeric(regions_name$region)
postcode_clean = postcode %>% clean_names()
#postcode_clean$pr = as.double(postcode_clean$pr)
postcode_province = postcode_clean %>% left_join(y = regions_name, by = c("pr", "region"))
#> Error: Join columns must be present in data.
#> x Problem with `region`.
> dput(head(postcode_clean, 10))
structure(list(pc = structure(c("A0A1A0", "A0A1B0", "A0A1C0",
"A0A1C0", "A0A1C0", "A0A1C0", "A0A1C0", "A0A1C0", "A0A1E0", "A0A1G0"
), label = "Postal code", format.spss = "A6"), pr = structure(c(10,
10, 10, 10, 10, 10, 10, 10, 10, 10), label = "Province or territory code", format.spss = "F2.0", display_width = 4L, labels = c(Newfoundland = 10,
`Prince Edward Island` = 11, `Nova Scotia` = 12, `New Brunswick` = 13,
Quebec = 24, Ontario = 35, Manitoba = 46, Saskatchewan = 47,
Alberta = 48, `British Columbia` = 59, Yukon = 60, `Northwest Territories` = 61,
Nunavut = 62), class = c("haven_labelled", "vctrs_vctr", "double"
)), cs_duid = structure(c(1001144, 1001464, 1001557, 1001557,
1001557, 1001557, 1001557, 1001557, 1001347, 1001409), label = "Census subdivision unique identifier", format.spss = "F7.0", display_width = 9L)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
> dput(head(regions_name, 10))
structure(list(region = c(1, 35, 24, 59, 48, 46, 47, 12, 13,
10), name = c("Canada", "Ontario", "Quebec", "British Columbia",
"Alberta", "Manitoba", "Saskatchewan", "Nova Scotia", "New Brunswick",
"Newfoundland and Labrador")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"), last_updated = structure(1648783776.07826, class = c("POSIXct",
"POSIXt")))
I don't understand it. I'm not doing anything complicated, yet I am being thrown this error. Any suggestions?

Build curves of populations in function of time

In my work i'm studying a lot of varieties of maize.
I would like to determinate the area under the curve during flowering (male and female) of these varieties.
I used the package DescTools and the function AUC (area under the curve). I converted my dates as a numeric vector. So my scipt is:
a<-XAUC$Date.flowering.male
b<-XAUC$Date.flowering.female
c<- XAUC$....
Here is my issue, because i would like to identify c as the population as function of time. How can i do this?
In this picture: the first graph is what i have and the second is what i would like to have.
and then the end of my script will be:
AUCfemale<-AUC(b,c,method = c("trapezoid"))
AUCmale<-AUC(a,c,method = c("trapezoid"))
Airdiff<-AUCmale-AUCfemale
Data
XAUC <- structure(list(Varietes = c("Abelastone", "Abelastone", "Abelastone", "Abelastone", "Abelastone"), ligne.rep = c(1, 1, 1, 1, 1), Pied = c(1, 2, 3, 6, 7), `Date.floraison.mâle` = c(7.29, 8.02, 8.01, 8.03, 8.04), Date.floraison.femelle = c(8.1, 8.17, 8.11, 8.25, 8.17 ), ASIi = c(12, 15, 10, 22, 13), Hauteur.des.pieds = c(230, 228, 226, 240, 233), Hauteur.des.soies = c(123, 118, 116, 124, 122), Date.floraison.mâle.graph = c(29, 33, 32, 34, 35), Date.floraison.femelle.graph = c(41, 48, 42, 56, 48)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"), na.action = structure(c("6" = 6L, "10" = 10L, "20" = 20L, "21" = 21L, "24" = 24L), class = "omit"))

how to plot two graphs together while share the same scale of x-axis

I would like to plot two graph together which share the same x-axis. How can I do that?
My data can be build using codes:
df <-structure(list(SDTM_LabN = c("ALP", "AST", "ALT", "AST", "ALT",
"ALT", "ALP", "AST", "ALP", "AST", "ALP", "ALT", "ALP", "ALP",
"ALT", "AST", "ALT", "ALT", "ALT", "AST", "AST", "ALP", "AST",
"ALT", "ALP", "ALP", "AST"), ADY = structure(c(45, 15, 1, 1,
30, 58, 30, 45, 46, -6, 23, 46, -6, 15, 23, 46, 45, -6, 8, 30,
58, 58, 23, 15, 8, 1, 8), class = "difftime", units = "days"),
result = c(0.841269841269841, 0.578947368421053, 0.625, 0.552631578947368,
0.416666666666667, 0.3125, 0.936507936507937, 0.447368421052632,
0.634920634920635, 0.657894736842105, 0.873015873015873,
0.291666666666667, 0.73015873015873, 0.857142857142857, 0.5,
0.447368421052632, 0.479166666666667, 0.625, 0.604166666666667,
0.5, 0.526315789473684, 0.849206349206349, 0.526315789473684,
0.5, 1.00793650793651, 0.896825396825397, 0.894736842105263
)), row.names = c(NA, -27L), class = "data.frame")
df2<-structure(list(ID = c(101, 101, 101, 101, 101, 101), AEDECOD = c("Diarrhoea",
"Vitreous floaters", "Musculoskeletal pain", "Diarrhoea", "Decreased appetite",
"Fatigue"), AESTDY = structure(c(101, 74, 65, 2, 33, 27), class = "difftime", units = "days"),
AEENDY = structure(c(105, 99, NA, 5, NA, NA), class = "difftime", units = "days")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
My plots codes are:
ggplot(df, aes(colour=SDTM_LabN)) +
geom_line(aes(x=ADY,y=result))
ggplot(df2, aes(colour=AEDECOD)) +
geom_segment(aes(x=AESTDY, xend=AEENDY, y=AEDECOD, yend=AEDECOD),) +
xlab("Duration")
How can I get sth that looks like this:
You should first make sure to calculate common xmin-xmax to both series.
Then with patwhwork a suggested in comments or cowplot:
xmin <- min(df$ADY ,df2$AESTDY)
xmax <- max(df$ADY ,df2$AESTDY)
p1 <- ggplot(df, aes(colour=SDTM_LabN)) +
geom_line(aes(x=ADY,y=result)) +
coord_cartesian(xlim = c(xmin,xmax))
p2 <- ggplot(df2, aes(colour=AEDECOD)) +
geom_segment(aes(x=AESTDY, xend=AEENDY, y=AEDECOD, yend=AEDECOD),) +
xlab("Duration") +
coord_cartesian(xlim = c(xmin,xmax))
library(cowplot)
plot_grid(plotlist = list(p1,p2),align='v',ncol=1)

Resources