Forcing tableGrob() to fit in .tiff file - r

I am trying to output a tableGrob into a .tiff, but it is a bit long. I've tried to get the area of the table fit into the .tiff file, but it is always cut off unless I drastically change the font size to an unreadable size. Is there a way to force the tableGrob to fin into the .tiff file without any cut off?
table.plot <- structure(list(A = c(2.8, 0.5, 1.3, 5.7, 6.5, 1.1, 3.3, 1, 16.9,
8.6, 6.3, 22.2, 14.8, 1.3, 7.9, 12.4, 31, 9.9, 13.2, 26.2, 2),
B = c(13.7, 10.6, 12.7, 20.6, 13.2, 11.2, 14.7, 11.7, 22.3,
12.9, 12.9, 19.5, 20.6, 11.1, 17, 20.3, 43.1, 18.2, 20.9,
26.7, 10.1), C = c(0.4, 0, 0.3, 1, 0.3, 0.1, 0.5, 0.2, 1.2,
0.3, 0.3, 0.9, 1, 0.1, 0.7, 1, 3.3, 0.8, 1.1, 1.6, 0), D = c(29.7,
18, 23.9, 46.2, 33.1, 20.1, 32.6, 21.1, 93.1, 39.9, 33, 116.8,
76.1, 21.4, 49, 66.7, 166.5, 53.7, 69.1, 127.4, 21)), row.names = c("G01",
"G02", "G03", "G04", "G05", "G06", "G07", "G08", "G09", "G10",
"G11", "G12", "H01", "H02", "H03", "H04", "H05", "H06", "H07",
"H08", "Host.1"), class = "data.frame")
tt1 <- ttheme_minimal(
core = list(fg_params = list(fontsize = 8),
padding = unit(c(0.1, 0.1), "mm")),
colhead = list(bg_params = list(fill = "white"),
fg_params = list(fontsize = 8, fontface = "bold")),
rowhead = list(fg_params = list(fontsize = 8, fontface = "bold"))
)
g <- tableGrob(table.plot, theme = tt1)
g <- gtable_add_grob(g,
grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
t = 2, b = nrow(g), l = 1, r = ncol(g))
g <- gtable_add_grob(g,
grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
t = 1, l = 1, r = ncol(g))
save_plot("table is cut off.tiff", g, dpi = 300)

Assuming that save_plot comes from the cowplot package (there is more than one package with a function called save_plot), you can specify the height and aspect ratio like this:
save_plot("table isnt cut off.tiff", g, dpi = 300, base_height = 5.5, base_asp = 0.4)

Related

Whats the equivalent of data_color from gt package in kable?

I am trying to do this table in kable package:
To do this, I am using data_color function from gt package.
I would like to know if there is any option to do the same in kable package.
My code:
tbl_ipca %>%
gt() %>%
data_color(
columns = 1:3,
colors = scales::col_numeric(
colorspace::diverge_hcl(n = 20, palette = "Blue-Red 3"),
reverse = FALSE,
domain = c(-max(abs(tbl_ipca[1:3])), max(abs(tbl_ipca[1:3])))
)
) %>%
data_color(
columns = 4:6,
colors = scales::col_numeric(
colorspace::diverge_hcl(n = 20, palette = "Blue-Red 3"),
reverse = FALSE,
domain = c(-max(abs(tbl_ipca[4:6])), max(abs(tbl_ipca[4:6])))
)
) %>%
data_color(
columns = 7:9,
colors = scales::col_numeric(
colorspace::diverge_hcl(n = 20, palette = "Blue-Red 3"),
reverse = FALSE,
domain = c(-max(abs(tbl_ipca[7:9])), max(abs(tbl_ipca[7:9])))
)
) %>%
data_color(
columns = 10:12,
colors = scales::col_numeric(
colorspace::diverge_hcl(n = 20, palette = "Blue-Red 3"),
reverse = FALSE,
domain = c(-max(abs(tbl_ipca[10:12])), max(abs(tbl_ipca[10:12])))
)
)
My dput():
structure(list(`Set/21` = c(1.1, 1.1, 1.2, 0.9, 1.9, 0.7, 1.3,
0.3, 0.6), `Out/21` = c(1.2, 1.4, 1.2, 1.2, 1.4, 1.2, 1.3, 1.6,
1), `Nov/21` = c(0.6, 1.7, 0.9, 0.4, 2.3, 0.3, 0, 1, 0.3), `Set/21` = c(2.8,
3.5, 3, 2.4, 4.6, 2.2, 3.2, 2.1, 1.7), `Out/21` = c(3.4, 3.9,
3.3, 3, 4.3, 2.6, 3.9, 2.9, 2.1), `Nov/21` = c(2.9, 4.2, 3.4,
2.6, 5.7, 2.3, 2.6, 2.9, 2), `Set/21` = c(5.1, 5.9, 4.8, 3.6,
8.1, 2.4, 5, 4.6, 1.8), `Out/21` = c(5.9, 6.5, 5.7, 4.5, 9.2,
3.5, 6, 5.7, 2.8), `Nov/21` = c(5.5, 7.2, 5.9, 4.6, 9.4, 4, 5.2,
5.8, 3.3), `Set/21` = c(12.5, 11, 10.2, 8.4, 15.7, 5.4, 13.9,
7.8, 4.4), `Out/21` = c(12, 11.5, 10.7, 8.5, 17, 6, 13.1, 8.4,
4.9), `Nov/21` = c(10.8, 12.4, 10.7, 7.8, 19.2, 5.7, 10.4, 9.2,
4.8)), row.names = c("Comercializáveis", "Duráveis", "IPCA",
"Livres", "Monitorados", "Não Comercializáveis", "Não Duráveis",
"Semi-Duráveis", "Serviços"), class = "data.frame")

DPLYR error: argument is not interpretable as logical

I want to subset glioma samples as scf.glioma dataframe from the scf dataframe by removing any columns that begin with NB, which is saved as scf.control. I tried to use the select function in the dplyr library to filter out any scf.control samples from the scf dataframe but it produced an error : argument is not interpretable as logical.
library(dplyr)
scf.ann <- ann[ann$Gene.symbol=="KITLG",]
scf <- dat.filtered[rownames(dat.filtered) %in% rownames(scf.ann),]
scf.control <- scf %>% select(starts_with("NB"))
scf.glioma <- select(scf, -one_of(colnames(scf.control)) %>% colnames(scf))
Error: argument is not interpretable as logical Run
rlang::last_error() to see where the error occurred. In addition:
Warning message: In if (do.NULL) NULL else if (nc > 0L) paste0(prefix,
seq_len(nc)) else character() : the condition has length > 1 and
only the first element will be used
scf.glioma <- as.numeric(unlist(scf.glioma))
> dput(scf)
structure(list(NB_GSM97800 = c(116.2, 5.5, 2.8, 1867.7), NB_GSM97803 = c(72.8,
29, 2.7, 1321.4), NB_GSM97804 = c(89.5, 18.1, 5, 1275.8), NB_GSM97805 = c(58.6,
27.4, 4.4, 1226), NB_GSM97807 = c(71, 28.4, 0.8, 1431.1), NB_GSM97809 = c(62.7,
39.5, 1, 863.2), NB_GSM97811 = c(97.5, 82.7, 5.8, 1458.4), NB_GSM97812 = c(116.9,
80, 5.4, 1627.5), NB_GSM97816 = c(9.8, 10.5, 3.6, 470.5), NB_GSM97817 = c(148.9,
18.8, 10.1, 1224.8), NB_GSM97820 = c(121.1, 76.3, 5.6, 1192.6
), NB_GSM97825 = c(128, 83.1, 2.5, 1724.6), NB_GSM97827 = c(157.6,
101.2, 2.5, 1927.2), NB_GSM97828 = c(60.3, 5.8, 2.4, 425.2),
NB_GSM97833 = c(111.6, 84.3, 1.8, 1346.4), NB_GSM97834 = c(62.1,
29.1, 1.7, 1443.2), NB_GSM97840 = c(67, 84.1, 2.3, 1126.4
), NB_GSM97846 = c(131.3, 45.3, 1.7, 1682.3), NB_GSM97848 = c(116.3,
28.7, 11.5, 1516), NB_GSM97849 = c(141.7, 105.6, 1.7, 1854.9
), NB_GSM97850 = c(97.3, 62.6, 6, 1272.7), NB_GSM97853 = c(84.3,
89, 42.1, 1551.1), NB_GSM97855 = c(56.6, 7, 5.2, 559.8),
II_GSM97878 = c(88.7, 2.9, 14.7, 858), II_GSM97913 = c(184.4,
73.5, 2.9, 1223.3), II_GSM97932 = c(81.6, 39.8, 42.8, 1802.5
), II_GSM97939 = c(61.5, 11.6, 3.9, 462.4), II_GSM97951 = c(67.2,
37.8, 3.5, 1165.8), II_GSM97957 = c(101.8, 5.3, 2.3, 669.1
), II_GSM97972 = c(75.4, 30.7, 2.1, 1041.2), III_GSM97793 = c(203.2,
44.2, 3.6, 2168.4), III_GSM97795 = c(90, 84.1, 3.3, 1338.6
), III_GSM97802 = c(109.2, 3.3, 2.3, 206.5), III_GSM97810 = c(130.3,
90.1, 1.9, 2442.2), III_GSM97815 = c(154, 46.1, 4.1, 1500.2
), III_GSM97837 = c(164.6, 54.1, 2.6, 1630.4), III_GSM97843 = c(188.7,
142.1, 2.5, 1523.7), III_GSM97890 = c(60.2, 7, 5.2, 566.8
), III_GSM97899 = c(127.8, 11.1, 2, 1796.6), III_GSM97910 = c(28,
2.6, 1.8, 1433.3), III_GSM97916 = c(138.6, 79.3, 1.3, 1326.7
), III_GSM97920 = c(16.7, 13.9, 2.1, 495.1), III_GSM97921 = c(181.6,
107.1, 3.5, 2362.4), III_GSM97927 = c(81.6, 66, 1, 1127),
III_GSM97937 = c(78.6, 51.4, 1.2, 1382), III_GSM97941 = c(177.5,
52.1, 71.8, 1301.9), III_GSM97943 = c(70.1, 41.7, 3.5, 1822.6
), III_GSM97958 = c(167.8, 56.4, 10.2, 1789.2), III_GSM97960 = c(49.6,
36.9, 4.2, 1210), IV_GSM97794 = c(186.2, 152.3, 6, 1671.8
), IV_GSM97796 = c(189.9, 122.4, 3.2, 1539.2), IV_GSM97797 = c(196.6,
100.8, 1.1, 963.7), IV_GSM97798 = c(367.8, 308.4, 1.4, 2131
), IV_GSM97801 = c(132.1, 12, 3.1, 955.1), IV_GSM97806 = c(191.8,
95.6, 4.8, 1085.6), IV_GSM97808 = c(145.1, 151.5, 21, 2052.2
), IV_GSM97813 = c(146.6, 89.3, 1, 1103.9), IV_GSM97814 = c(224.4,
78.6, 2.9, 1890.6), IV_GSM97818 = c(40.7, 11.6, 1.7, 1737.7
), IV_GSM97819 = c(200.9, 106.9, 2, 1504.8), IV_GSM97821 = c(152.6,
94, 2.5, 1180), IV_GSM97826 = c(67.6, 47.4, 22.7, 810.3),
IV_GSM97829 = c(103.1, 61.5, 1.4, 1609.4), IV_GSM97832 = c(76.1,
28, 2.2, 1281.6), IV_GSM97836 = c(133.3, 43.1, 2.5, 1257.7
), IV_GSM97839 = c(121.2, 73.9, 1.8, 1756.5), IV_GSM97844 = c(135,
82.5, 6.6, 1624.8), IV_GSM97847 = c(137.7, 41.4, 5.7, 1080.4
), IV_GSM97851 = c(151.8, 140, 1.4, 1668.9), IV_GSM97852 = c(96.4,
118.2, 4.3, 830.6), IV_GSM97856 = c(160, 44.8, 19.4, 1824.3
), IV_GSM97858 = c(102.4, 9.3, 4.5, 1254.7), IV_GSM97859 = c(118.5,
27.8, 3.7, 577.7), IV_GSM97861 = c(181.2, 119.8, 4.4, 2091.7
), IV_GSM97863 = c(168.9, 77.9, 2.2, 1578.3), IV_GSM97869 = c(99.1,
67.5, 2.9, 984.3), IV_GSM97870 = c(81.9, 52.5, 4.3, 914.6
), IV_GSM97871 = c(142.6, 132.3, 2.6, 1445.3), IV_GSM97877 = c(85.6,
65.9, 3.4, 1508.7), IV_GSM97879 = c(56.8, 13.9, 1.3, 1456.5
), IV_GSM97882 = c(153.8, 91.6, 2, 876.2), IV_GSM97885 = c(81.1,
106.5, 1, 1240.7), IV_GSM97886 = c(66.1, 13.9, 5.4, 780.6
), IV_GSM97887 = c(111.4, 3.7, 4.4, 609.2), IV_GSM97888 = c(111.8,
34.3, 3.7, 804.8), IV_GSM97889 = c(121, 78.6, 1.2, 1204.5
), IV_GSM97891 = c(118.4, 8, 4.2, 1047), IV_GSM97892 = c(141.5,
103, 40.1, 1186.8), IV_GSM97893 = c(145.6, 49.8, 2.6, 2324
), IV_GSM97894 = c(143.6, 111.9, 8.1, 2007.1), IV_GSM97896 = c(118.3,
41.4, 8.4, 1503.7), IV_GSM97898 = c(190.8, 178.3, 2.9, 2856.6
), IV_GSM97903 = c(159.6, 45.2, 4.3, 1174.1), IV_GSM97905 = c(97.9,
38.6, 3.7, 1422), IV_GSM97906 = c(110.4, 102, 1.5, 1360.6
), IV_GSM97908 = c(76.7, 15.4, 4.4, 2207.8), IV_GSM97912 = c(43.6,
6.8, 2.2, 778.1), IV_GSM97914 = c(95.3, 3.9, 1, 905.6), IV_GSM97915 = c(193.9,
119.6, 5.5, 1666.4), IV_GSM97917 = c(802.8, 552, 3.5, 3560.7
), IV_GSM97918 = c(131.5, 80.8, 3.1, 1964.2), IV_GSM97919 = c(262.6,
254.8, 4.6, 1556), IV_GSM97922 = c(138.6, 67.5, 3.7, 770.5
), IV_GSM97924 = c(99.8, 12.3, 4.1, 879), IV_GSM97926 = c(102.5,
14.3, 1.8, 1142.9), IV_GSM97930 = c(70.6, 21.5, 4, 1740.5
), IV_GSM97931 = c(130.9, 41.7, 23.8, 558.8), IV_GSM97935 = c(121.6,
53.8, 1.6, 1180.3), IV_GSM97936 = c(121.8, 79.1, 2.7, 1830.8
), IV_GSM97938 = c(160.9, 109.4, 1.9, 1714.8), IV_GSM97940 = c(191.9,
96.7, 2.5, 1562.9), IV_GSM97942 = c(211.2, 140.6, 5.4, 1276.2
), IV_GSM97945 = c(108.8, 86.9, 1.8, 1329.3), IV_GSM97946 = c(87,
47.3, 1.1, 1587.7), IV_GSM97948 = c(117.5, 42.7, 5.4, 778.6
), IV_GSM97950 = c(260.6, 237.2, 6.9, 3320.4), IV_GSM97952 = c(248.7,
166.3, 7.8, 1446.4), IV_GSM97953 = c(173.1, 88.4, 2.6, 5287.1
), IV_GSM97954 = c(71.8, 49.4, 2.8, 1334.8), IV_GSM97955 = c(83.4,
79.8, 21.1, 919.8), IV_GSM97959 = c(100.6, 52.2, 9.2, 1607.6
), IV_GSM97961 = c(86.7, 25.8, 2.3, 1600.9), IV_GSM97963 = c(105.9,
62.7, 1.2, 933), IV_GSM97965 = c(119.9, 77.2, 4.9, 1959.5
), IV_GSM97966 = c(74.4, 50.9, 6.3, 804.2), IV_GSM97967 = c(56.9,
28.6, 1.7, 2295), IV_GSM97968 = c(74.1, 9.4, 6.2, 1142.8),
IV_GSM97969 = c(56, 67.1, 6.7, 1057.5), IV_GSM97971 = c(113.6,
52.7, 1.2, 923.4), II_GSM97799 = c(197.5, 93.3, 22.6, 1552.4
), II_GSM97823 = c(133.6, 8.7, 1.4, 1545.7), II_GSM97824 = c(66.1,
52.9, 1.1, 2014), II_GSM97830 = c(91.2, 5.3, 3.3, 613.5),
II_GSM97835 = c(109.5, 43.6, 1.9, 1556.4), II_GSM97838 = c(108.1,
50.9, 3.4, 1515.2), II_GSM97841 = c(79.2, 89.3, 4, 2068.6
), II_GSM97842 = c(149.1, 27.9, 1.4, 1190.1), II_GSM97854 = c(74.5,
49.5, 4.4, 1121.1), II_GSM97857 = c(97.4, 10.7, 5.3, 1591.9
), II_GSM97860 = c(150.3, 16.5, 14, 1937.4), II_GSM97862 = c(48.8,
19.9, 3.7, 970.9), II_GSM97864 = c(108, 51, 26.3, 1659.9),
II_GSM97866 = c(87.1, 99.9, 5, 1114.7), II_GSM97868 = c(113.6,
50.7, 2.7, 780.5), II_GSM97872 = c(62.4, 23.5, 0.8, 856.3
), II_GSM97873 = c(116.5, 42.8, 1.9, 884.9), II_GSM97874 = c(145.4,
65.3, 2.9, 1653.4), II_GSM97875 = c(196.2, 83.8, 4.7, 1884.1
), II_GSM97876 = c(138.2, 28.5, 2.9, 1599.9), II_GSM97880 = c(150.8,
11.3, 15.1, 2020.6), II_GSM97881 = c(101, 66, 8.1, 919),
II_GSM97884 = c(119, 62, 5.9, 2846.2), II_GSM97901 = c(98.8,
2.9, 15.4, 1469.7), II_GSM97902 = c(80, 44.9, 7.2, 1638.8
), II_GSM97909 = c(141.9, 66.6, 4.5, 984.5), II_GSM97911 = c(139.4,
8.9, 33.4, 1045.6), II_GSM97923 = c(198.2, 81.6, 25.4, 1875.6
), II_GSM97928 = c(80.1, 10.2, 14.2, 432.1), II_GSM97929 = c(97.8,
104.4, 8, 2026.4), II_GSM97933 = c(99.2, 51.3, 1.6, 2814.5
), II_GSM97934 = c(120.7, 46.4, 5.8, 2613.3), II_GSM97944 = c(116.6,
65.9, 7.6, 1492.6), II_GSM97949 = c(104.2, 76.1, 6.9, 2139.2
), II_GSM97956 = c(88.5, 7.1, 1, 1255.5), II_GSM97962 = c(56,
5.2, 3.6, 807.8), II_GSM97964 = c(90.5, 42.6, 1.9, 986.8),
II_GSM97970 = c(205.3, 80, 3, 1313.8), III_GSM97822 = c(85,
53.1, 4.2, 562.7), III_GSM97831 = c(94.2, 39.8, 4.3, 2129.4
), III_GSM97845 = c(147.7, 116.4, 1.1, 1918.8), III_GSM97865 = c(98.4,
29.6, 7.1, 1588.5), III_GSM97867 = c(126.2, 21.2, 2.5, 1551.4
), III_GSM97883 = c(53.4, 12.3, 1.3, 842.6), III_GSM97897 = c(133.6,
49, 1.6, 740.4), III_GSM97900 = c(150.3, 30.6, 3.8, 832),
III_GSM97904 = c(131.4, 13.8, 30.7, 2116.2), III_GSM97907 = c(87.4,
114.3, 6.3, 1129.7), III_GSM97925 = c(83.5, 17.5, 2.9, 460.9
), III_GSM97947 = c(103.7, 12.2, 2.2, 527.5)), row.names = c("207029_at",
"211124_s_at", "216974_at", "226534_at"), class = "data.frame")
> dput(scf.control)
structure(list(NB_GSM97800 = c(116.2, 5.5, 2.8, 1867.7), NB_GSM97803 = c(72.8,
29, 2.7, 1321.4), NB_GSM97804 = c(89.5, 18.1, 5, 1275.8), NB_GSM97805 = c(58.6,
27.4, 4.4, 1226), NB_GSM97807 = c(71, 28.4, 0.8, 1431.1), NB_GSM97809 = c(62.7,
39.5, 1, 863.2), NB_GSM97811 = c(97.5, 82.7, 5.8, 1458.4), NB_GSM97812 = c(116.9,
80, 5.4, 1627.5), NB_GSM97816 = c(9.8, 10.5, 3.6, 470.5), NB_GSM97817 = c(148.9,
18.8, 10.1, 1224.8), NB_GSM97820 = c(121.1, 76.3, 5.6, 1192.6
), NB_GSM97825 = c(128, 83.1, 2.5, 1724.6), NB_GSM97827 = c(157.6,
101.2, 2.5, 1927.2), NB_GSM97828 = c(60.3, 5.8, 2.4, 425.2),
NB_GSM97833 = c(111.6, 84.3, 1.8, 1346.4), NB_GSM97834 = c(62.1,
29.1, 1.7, 1443.2), NB_GSM97840 = c(67, 84.1, 2.3, 1126.4
), NB_GSM97846 = c(131.3, 45.3, 1.7, 1682.3), NB_GSM97848 = c(116.3,
28.7, 11.5, 1516), NB_GSM97849 = c(141.7, 105.6, 1.7, 1854.9
), NB_GSM97850 = c(97.3, 62.6, 6, 1272.7), NB_GSM97853 = c(84.3,
89, 42.1, 1551.1), NB_GSM97855 = c(56.6, 7, 5.2, 559.8)), row.names = c("207029_at",
"211124_s_at", "216974_at", "226534_at"), class = "data.frame")
Try this:
To remove:
library(dplyr)
scf.glioma <- scf %>%
select(-starts_with("NB"))
To subset:
scf.control <- scf %>%
select(starts_with("NB"))

How to add a color legend to a ggplot with different geom layers

I have searched and found several examples of what I thought were solutions to this issue, but when I implement them nothing seems to work.
I have a tibble, and have created a graph which consists of two geom_lines with error bars. I want to create a legend at the side that says 'Limb A' beside a red line, and 'Limb B' beside a blue line. Absolutely nothing I do seems to work at all despite feeling like I modify the code each time according the reasonable solutions that come up when I search.
The plot that I want to add a legend to
So, here is my code and an example. If anyone can guide me I would hugely appreciate it. My assumption is that I am missing something pretty fundamental but I am completely lost.
compliance_graph <- ggplot(compliance_stats) +
aes(x = Compliance_B_L_per_cmH2O, y = Mean_VtB) +
geom_line(colour = 'deepskyblue', size = 0.75) +
geom_point(colour = 'deepskyblue', size = 1.0) +
geom_errorbar(aes(ymin = (Mean_VtB - SD_VtB),
ymax = (Mean_VtB + SD_VtB),
width= 0.003),
colour = 'deepskyblue') +
geom_line(aes(y = Mean_VtA, colour = 'Limb A'), colour = 'firebrick4', size = 0.75) +
geom_point(aes(y = Mean_VtA), colour = 'firebrick4', size = 1) +
geom_errorbar(aes(ymin = (Mean_VtA - SD_VtA),
ymax = (Mean_VtA + SD_VtA),
width= 0.003),
colour = 'firebrick4') +
labs(
title = 'Compliance of Limb A vs Tidal Volume',
x = 'Limb A Compliance (L/cmH2O)',
y = 'Mean Tidal Volume (mL)')
compliance_graph <- compliance_graph +
scale_x_continuous(expand = c(0, 0), breaks = breaks_width(0.01)) +
scale_y_continuous(expand = c(0, 0), breaks = breaks_width(50), limits = c(150, 450))
dput(compliance_stats) returns the following:
structure(list(Compliance_B_L_per_cmH2O = c(0.01, 0.02, 0.03,
0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14,
0.15), Vent_P1 = c(10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10), Vent_PEEP = c(5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5), Vent_RR = c(14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14), Vent_IE = c("1:2", "1:2", "1:2", "1:2",
"1:2", "1:2", "1:2", "1:2", "1:2", "1:2", "1:2", "1:2", "1:2",
"1:2", "1:2"), APL_pos = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3), Mean_VtA = c(344, 359, 377, 380, 379, 382, 377, 377,
377, 376, 375, 374, 375, 375, 376), Min_VtA = c(342, 358, 374,
379, 375, 380, 376, 375, 375, 375, 372, 373, 374, 373, 373),
Max_VtA = c(345, 360, 380, 381, 385, 385, 378, 378, 379,
377, 377, 376, 376, 376, 379), SD_VtA = c(2, 1, 3, 1, 5,
3, 1, 2, 2, 1, 3, 2, 1, 2, 3), Mean_PEEPA = c(6.9, 7, 7.1,
7.2, 7.3, 7.3, 7.4, 7.4, 7.5, 7.5, 7.4, 7.4, 7.4, 7.5, 7.4
), Min_PEEPA = c(6.9, 6.9, 7, 7.2, 7.3, 7.3, 7.4, 7.4, 7.4,
7.4, 7.4, 7.3, 7.3, 7.4, 7.3), Max_PEEPA = c(6.9, 7, 7.1,
7.2, 7.4, 7.4, 7.5, 7.5, 7.5, 7.6, 7.5, 7.6, 7.5, 7.6, 7.4
), SD_PEEPA = c(0, 0.1, 0.1, 0, 0.1, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.2, 0.1, 0.1, 0.1), Mean_PeakA = c(14.2, 14.7,
15.4, 15.9, 16.1, 16.3, 16.3, 16.4, 16.4, 16.5, 16.4, 16.5,
16.4, 16.5, 16.5), Min_PeakA = c(14.2, 14.6, 15.3, 15.9,
16.1, 16.2, 16.1, 16.3, 16.3, 16.4, 16.4, 16.5, 16.4, 16.4,
16.4), Max_PeakA = c(14.2, 14.7, 15.5, 15.9, 16.1, 16.3,
16.4, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.6), SD_PeakA = c(0,
0.1, 0.1, 0, 0, 0.1, 0.2, 0.1, 0.1, 0.1, 0.1, 0, 0.1, 0.1,
0.1), Mean_VtB = c(167, 229, 319, 362, 386, 396, 401, 405,
407, 409, 409, 412, 412, 415, 414), Min_VtB = c(166, 228,
316, 358, 385, 393, 399, 404, 406, 409, 408, 411, 410, 412,
413), Max_VtB = c(168, 230, 321, 364, 386, 400, 402, 405,
408, 410, 410, 412, 414, 417, 416), SD_VtB = c(1, 1, 3, 3,
1, 4, 2, 1, 1, 1, 1, 1, 2, 3, 2), Mean_PEEPB = c(6.4, 6.4,
6.5, 6.6, 6.9, 7, 7.1, 7.2, 7.1, 7.2, 7.2, 7.3, 7.3, 7.3,
7.3), Min_PEEPB = c(6.3, 6.3, 6.4, 6.5, 6.7, 6.9, 7, 7.1,
7, 7.1, 7.1, 7.1, 7.2, 7.2, 7.2), Max_PEEPB = c(6.4, 6.5,
6.6, 6.7, 7, 7.1, 7.3, 7.3, 7.3, 7.3, 7.3, 7.5, 7.5, 7.5,
7.5), SD_PEEPB = c(0.1, 0.1, 0.1, 0.1, 0.2, 0.1, 0.2, 0.1,
0.2, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2), Mean_PeakB = c(18, 18,
17.9, 17.8, 17.8, 17.7, 17.5, 17.5, 17.5, 17.4, 17.4, 17.4,
17.3, 17.2, 17.2), Min_PeakB = c(17.9, 18, 17.8, 17.8, 17.7,
17.6, 17.4, 17.4, 17.3, 17.3, 17.3, 17.2, 17.1, 17.1, 17),
Max_PeakB = c(18.2, 18.1, 18, 17.9, 17.8, 17.8, 17.6, 17.5,
17.7, 17.5, 17.5, 17.6, 17.4, 17.4, 17.4), SD_PeakB = c(0.2,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.1, 0.1, 0.2, 0.2,
0.2, 0.2)), row.names = c(NA, -15L), groups = structure(list(
Compliance_B_L_per_cmH2O = c(0.01, 0.02, 0.03, 0.04, 0.05,
0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15
), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -15L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
If you want to have a legend you have to map on aesthetics, i.e. move color into aes() and make use of scale_color_manual to set the right colors:
library(ggplot2)
compliance_graph <- ggplot(compliance_stats) +
aes(x = Compliance_B_L_per_cmH2O, y = Mean_VtB) +
geom_line(aes(colour = "Limb B"), size = 0.75) +
geom_point(aes(colour = "Limb B"), size = 1.0) +
geom_errorbar(aes(
ymin = (Mean_VtB - SD_VtB),
ymax = (Mean_VtB + SD_VtB),
width = 0.003,
colour = "Limb B"
)) +
geom_line(aes(y = Mean_VtA, colour = "Limb A"), size = 0.75) +
geom_point(aes(y = Mean_VtA, colour = "Limb A"), size = 1) +
geom_errorbar(aes(
ymin = (Mean_VtA - SD_VtA),
ymax = (Mean_VtA + SD_VtA),
colour = "Limb A",
width = 0.003
)) +
scale_color_manual(values = c("Limb A" = "firebrick4", "Limb B" = "deepskyblue")) +
labs(
title = "Compliance of Limb A vs Tidal Volume",
x = "Limb A Compliance (L/cmH2O)",
y = "Mean Tidal Volume (mL)"
)
compliance_graph <- compliance_graph +
scale_x_continuous(expand = c(0, 0), breaks = scales::breaks_width(0.01)) +
scale_y_continuous(expand = c(0, 0), breaks = scales::breaks_width(50), limits = c(150, 450))
compliance_graph

How to average 7 years of data

I'm new to R programming and this website so please bear with my incompetence. I pulled atmospheric data from the past 7 years for 7 variables; ozone, CO, NO, NO2, windspeed, PM 2.5, PM 10. What I am trying to do is graph this data to see if the government mandated stay at home orders during this covid-19 pandemic had any effect on atmospheric composition. From the graph I'm not quite sure what to do next. I believe I need an average of the past 7 years? The data is hourly data displayed in a 24 hour format.
NO.dat data frame
dput(head(NO.dat,10))
structure(list(Date = c("3/1/2014", "3/2/2014", "3/3/2014", "3/4/2014",
"3/5/2014", "3/6/2014", "3/7/2014", "3/8/2014", "3/9/2014", "3/10/2014"
), X0.00 = c(3.6, NA, 2.3, 17.1, 0.4, 0.9, 110.9, 0.1, NA, 0.4
), X1.00 = c(6.3, NA, 1.4, 18.7, 0.2, 0.2, 15.8, 0, NA, 0.6),
X2.00 = c(2.3, 0.6, 0.4, 13.9, 0.2, 0.1, 13.5, 0, 0.8, 0.3
), X3.00 = c(0.9, 0.3, 0.9, 4.2, 0.5, 0.3, 22.7, 0.2, 0.5,
0.7), X4.00 = c(0.2, 0.1, 2.8, 5.2, 0.7, 0, 40.1, 0.1, 0.8,
2.8), X5.00 = c(0.4, 0, 4.4, 11.1, 2.4, 1.8, 22.2, 0.1, 0.8,
4.1), X6.00 = c(11.8, 0.1, 17.6, 51.8, 3.6, 8.2, 2.8, 0.3,
1, 20.1), X7.00 = c(39.5, 0.6, 30.3, 118.6, 15.7, 12, 3.7,
1, 1.9, 39.1), X8.00 = c(23.9, 0.7, 25.8, 35.6, 20.6, 11.4,
6.3, 1.5, 1.2, 33.5), X9.00 = c(8.4, 1.1, 20.8, 28.7, 5.1,
9.4, 3.7, 1.3, 0.8, 9.6), X10.00 = c(4.3, 0.5, 13.3, 17.1,
1.1, 6, 1.3, 2.4, 1.4, 2.5), X11.00 = c(3.9, 0.3, 8.3, 13.9,
0.5, 5.6, 0.9, 2.3, 1.3, 1.2), X12.00 = c(4.1, 0.6, 6.3,
12.2, 0.6, 4.3, 0.8, 1.6, 1, 1.1), X13.00 = c(2.6, 0.6, 9.1,
9, 0.6, 3.6, 0.7, 2, 1.6, 1.1), X14.00 = c(3.7, 0.5, 9.3,
1.4, 0.9, 2.3, 0.9, 1.1, 1.1, 1.3), X15.00 = c(3.4, 0.5,
9.4, 0.8, 0.8, 1.8, 1.2, 1.8, 1.2, 1.1), X16.00 = c(1, 0.3,
5.7, 0.6, 2.5, 2.3, 1.1, 2.3, 1.2, 1), X17.00 = c(0.9, 0.3,
13.4, 0.5, 3.2, 1.8, 0.7, 1.4, 0.6, 0.7), X18.00 = c(0.8,
0.2, 22.1, 0.5, 3.9, 0.7, 0.7, 0.9, 0.4, 0.5), X19.00 = c(0.5,
0.2, 24.2, 1.8, 15.4, 1.1, 0.1, 0.8, 10.1, 0.6), X20.00 = c(0.5,
1, 18.4, 17.1, 5.1, 33.4, 0.3, 0.1, 45.3, 0.5), X21.00 = c(1,
0.5, 15.4, 55.7, 2, 39.5, 4.1, 0, 49.5, 0.4), X22.00 = c(0.4,
0.2, 8.1, 52.6, 2.7, 25.2, 0.9, 0.3, 27.2, 0.5), X23.00 = c(0.4,
6, 11.9, 2.2, 2.5, 62.1, 0.2, 0.1, 3.3, 0.4)), row.names = c(NA,
10L), class = "data.frame")
NO2.dat data frame
dput(head(NO2.dat,10))
structure(list(Date = c("3/1/2014", "3/2/2014", "3/3/2014", "3/4/2014",
"3/5/2014", "3/6/2014", "3/7/2014", "3/8/2014", "3/9/2014", "3/10/2014"
), X0.00 = c(5, 0.5, 3.2, 16.3, 0.4, 2, 91.2, 0.2, 0.5, 0.2),
X1.00 = c(7, 0.4, 2.4, 18.4, 0.3, 0.6, 17.7, 0.2, 0.5, 0.1
), X2.00 = c(1.7, 0.4, 0.3, 16.3, 0.1, 0.4, 10.3, 0.3, 0.6,
0.2), X3.00 = c(0.8, 0.6, 0.7, 4.4, 0.8, 0.6, 8.5, 0.4, 0.5,
0.6), X4.00 = c(0.6, 0.2, 2.6, 4.4, 1, 0.6, 43.7, 0.3, 0.7,
2.6), X5.00 = c(0.6, 0.3, 5, 12.8, 2.7, 2.8, 15.7, 0.4, 0.7,
4.3), X6.00 = c(5.8, 0.4, 18.6, 60.5, 3.8, 9.5, 3, 0.6, 0.9,
22.1), X7.00 = c(32, 0.7, 27.4, 117.5, 15.3, 12.6, 4.4, 1.7,
2.2, 36.2), X8.00 = c(21.3, 1, 22.7, 37.1, 20.3, 12.5, 7.6,
2.1, 1.4, 33.2), X9.00 = c(7.9, 1.4, 19.4, 28.7, 5, 10.5,
4.8, 2.3, 0.9, 11), X10.00 = c(4.2, 0.6, 12.4, 19, 1.6, 8.1,
1.9, 3.1, 1.8, 2.9), X11.00 = c(4.2, 0.8, 9.6, 15.7, 1.1,
7.4, 1.6, 3.4, 1.8, 1), X12.00 = c(4.2, 0.9, 6.6, 14.2, 1.2,
6.1, 1.4, 2.7, 1.3, 1.2), X13.00 = c(NA, 0.8, 9.4, NA, 1.4,
3.9, 1.2, NA, 1.9, 1.2), X14.00 = c(NA, 0.9, 9.6, NA, 1.9,
3.1, 1.3, NA, 1.3, 1.3), X15.00 = c(NA, 0.9, 9.6, NA, 1.7,
2.9, 1.9, NA, 1.6, 1), X16.00 = c(1, 0.8, 6.4, 1.2, 3.8,
3, 1.8, 3, 1.2, 1.1), X17.00 = c(1.2, 0.7, 12, 1, 4, 1.5,
1.5, 2, 0.5, 0.6), X18.00 = c(0.9, 0.5, 20.2, 0.9, 5, 1,
1.3, 1.5, 0.3, 0.3), X19.00 = c(0.5, 0.5, 19.1, 2.1, 15.8,
1.1, 0.6, 1.3, 5.1, 0.4), X20.00 = c(0.4, 1.1, 17.5, 7, 4.2,
24.9, 0.5, 0.7, 32.1, 0.4), X21.00 = c(0.7, 0.7, 13.3, 28.4,
2.4, 31.7, 3.4, 0.7, 37, 0.3), X22.00 = c(0.4, 0.4, 7.3,
21, 2.9, 18.5, 1.2, 0.6, 20, 0.3), X23.00 = c(0.4, 5.8, 11.6,
0.8, 2.9, 47.8, 0.5, 0.6, 2.1, 0.2)), row.names = c(NA, 10L
), class = "data.frame")
Any help would be much appreciated!
'''
library(reshape2)
library(dplyr)
library(lubridate)
library(ggplot2)
#remove summary stats
NO.dat <- NO.dat[,1:25]
NO2.dat <- NO2.dat[,1:25]
#reorganize data using reshape
x<-melt(NO.dat, id="Date")
colnames(x) <- c("Date","Hour","NO")
x$Hour<- as.numeric(x$Hour)-y<-melt(NO2.dat, id="Date")
y<-melt(NO2.dat, id="Date")
colnames(y) <- c("Date","Hour","NO2")
y$Hour<- as.numeric(y$Hour)-1
x <- cbind(x,y$NO2)
colnames(x)[4] <- "NO2"
x$min <- ":00"
x$time <- paste(x$Hour, x$min, sep="")
x$DT <- paste(x$Date, x$time)
x %>% select(DT, NO, NO2) %>% mutate(NOx=NO + NO2) %>% mutate(DT =
mdy_hm(DT)) %>% arrange(DT) -> x
p <- ggplot(x, aes(x=DT, y=NOx)) + geom_line() + xlab("")
x$index <- 1:nrow(x)
loessMod10 <- loess(NOx ~ index, data=x, span=0.10, na.action=)
x <- na.omit(x)
x$smoothed10 <- predict(loessMod10)
#pdf("El Paso NOx.pdf",w=6,h=3,useDingbats= FALSE)
p <- ggplot(x) + geom_line(aes(x=DT, y=NOx), linetype = "dashed",
size=0.3) + xlab("") +
geom_line(aes(x=DT, y=smoothed10), color = "red") + labs(y="NOx
(ppbv)") +
ggtitle("NOx concentrations at Chamizal TCEQ Site") +
theme(plot.title = element_text(hjust = 0.5)) +
annotate("text",x=as.POSIXct("2020-03-24 17:00:00"), y=130, +
label="Stay Home 1", angle=90, size=2.5)
annotate("text",x=as.POSIXct("2020-04-01 17:00:00"), y=130,
label="Stay Home 2", angle=90, size = 2.5) +
annotate("segment", x = as.POSIXct("2020-03-24 17:00:00"), xend=
as.POSIXct("2020-03-24 17:00:00"), y = 0, yend = 105, colour =
"blue") +
annotate("segment", x = as.POSIXct("2020-04-01 17:00:00"), xend=
as.POSIXct("2020-04-01 17:00:00"), y = 0, yend = 105, colour =
"blue")
'''
Graph that I think needs to be averaged??
Perhaps you can add a year column and use this to group your data by year. You could then overlay the data by year.
For fun, I had a go at reproducing the plots avoiding reshape.
library(dplyr)
library(tidyr)
library(stringr)
library(ggplot2)
# stack raw data for NO and NO2
NO_stacked <- NO %>%
pivot_longer(cols = starts_with("X"),
names_to = "hours",
values_to = "NO")
NO2_stacked <- NO2 %>%
pivot_longer(cols = starts_with("X"),
names_to = "hours",
values_to = "NO2")
# combine into one data frame
data <- bind_cols(NO_stacked, NO2_stacked) %>%
select(Date, hours, NO, NO2)
# coerce dates to POSIXct and add hours; remove hours; reshape to long format using pivot_longer
data <- data %>%
mutate(Date = as.POSIXct(Date, format = "%m/%d/%Y", tz = "UTC"),
hours = as.numeric(str_sub(hours, start = 2, end = -1))) %>%
mutate(Date = Date + 60*60*hours) %>%
select(-hours) %>%
pivot_longer(cols = contains("NO"),
names_to = "Contaminant",
values_to = "Concentration")
# plot
ggplot(data = data, aes(x = Date, y = Concentration)) +
geom_line() +
geom_smooth(method = "loess", formula = y ~ x) +
facet_wrap(vars(Contaminant), nrow = 2) +
ggtitle("NOx concentrations at Chamizal TCEQ Site")

How to add line using other data to ggplot?

I would like to add a line to my data:
Part of my data:
dput(d[1:20,])
structure(list(MW = c(10.8, 10.9, 11, 11.7, 12.8, 16.6, 16.9,
17.1, 17.4, 17.6, 18.5, 19.1, 19.2, 19.7, 19.9, 20.1, 22.4, 22.7,
23.4, 24), Fold = c(21.6, 21.8, 22, 23.4, 25.6, 33.2, 33.8, 34.2,
34.8, 35.2, 37, 38.2, 38.4, 39.4, 39.8, 40.2, 44.8, 45.4, 46.8,
48), Column = c(33.95, 33.95, 33.95, 33.95, 33.95, 33.95, 33.95,
33.95, 33.95, 33.95, 33.95, 33.95, 33.95, 33.95, 33.95, 33.95,
33.95, 33.95, 33.95, 33.95), Bool = c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("MW", "Fold",
"Column", "Bool"), row.names = c(NA, 20L), class = "data.frame")
data for line:
> dput(bb[1:20,])
structure(c(1.95, 3.2, 3.7, 4.05, 4.5, 4.7, 4.75, 5.05, 5.2,
5.2, 5.2, 5.25, 5.3, 5.35, 5.35, 5.4, 5.4, 5.45, 5.5, 5.5, 10,
33.95, 58.66, 84.42, 110.21, 134.16, 164.69, 199.1, 234.35, 257.19,
361.84, 432.74, 506.34, 581.46, 651.71, 732.59, 817.56, 896.24,
971.77, 1038.91), .Dim = c(20L, 2L), .Dimnames = list(NULL, c("b",
"a")))
And as the last the code which I use to create this plot:
first_dot <- ggplot(d, aes(x=MW, y=Column)) +
geom_point() +
scale_x_continuous(limits=c(0, 650), breaks=c(0, 200, 400, 650)) +
scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))
first_dot + geom_line(bb, aes(x=b, y=a))
I get this error all the time when I try to run it:
Error: ggplot2 doesn't know how to deal with data of class uneval
Do you have any idea what I do wrong ?
That's how the data look like without line:
And how it should look like after adding like (approximately):
Just in case somebody lands here looking for an answer to the [underlying] question of "how to plot a line from a different data frame on top of an existing plot":
The key point here is to use the data= in the call to geom_line. Assuming that the data= is not required has happened to some of us, and often is the reason it doesn't work. Correct code would be, as #Roland put in the comments to the question:
ggplot(d, aes(x=MW, y=Column)) +
geom_point() +
scale_x_continuous(limits=c(0, 650), breaks=c(0, 200, 400, 650)) +
scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))+
geom_line(data=as.data.frame(bb),aes(x=b,y=a))
Unfortunately your bb variable is a numberical table so this cannot be plotted by ggplot. Could you try the following:
first_dot + geom_line(data=data.frame(bb), aes(x=b, y=a))
Please note that you convert the bb variable to a data.frame.

Resources