I have a r data frame and would like to make a dot plot in ggplot where x-axis would correspond to different groups present in Ensembl_ID row ("ENSG00000000003", "ENSG00000000005" etc. are the group for x-axis) and y-axis would correspond to respective numbers present in logFC.1 to logFC.9. I would like to display the logFC.1 to logFC.9 number of each respective group in form of dots like the attached image (to prevent confusion, each group should have the same color and only change with intensity like: if the mean x is bigger it gets more red and if the mean y is bigger it gets more green or something).
If possible then can you guys also help me in assigning the different color for positive and negative values.
Can we also assign the dot's color based on value (color intensity)?
df1 <- data.frame(Ensembl_ID = c("ENSG00000000003", "ENSG00000000005", "ENSG00000000419", "ENSG00000000457", "ENSG00000000460", "ENSG00000000938", "ENSG00000000971", "ENSG00000001036", "ENSG00000001084", "ENSG00000001167" ), logFC.1 = c(0.147447019707984, -0.278643924528991, 0.00638502079233481, 0.00248371473862579, 0.0591639590814736, -0.0892578080659792, -0.0139042150604349, 0.15210410748665, -0.0273174541997048, 0.0373813166759115 ), logFC.2 = c(0.14237211045168, -0.153847067952652, 0.00806519294435945, -0.0243298183425441, 0.0639184480028851, -0.0791126460573967, -0.0517704622015086, 0.100033161692714, 0.105136768894399, 0.0509474174745926 ), logFC.3 = c(0.0692402101693023, -0.212626837128185, 0.0665466667502187, 0.0189664498456434, 0.073631371224761, -0.0642014520794086, 0.0115060035255512, 0.104767159584613, 0.140378485980222, 0.0814931176279395), logFC.4 = c(0.175916688982428, -0.0606440302201137, 0.0862627141013101, 0.105179938123113, 0.128866411791584, -0.0988927171791539, 0.128758540724723, 0.0997656895899759, 0.345468063926355, 0.130898388184307), logFC.5 = c(0.144743421921328, 0.247159332221974, 0.0232237466183996, 0.0800788300610377, 0.178887735169961, -0.0592727391427514, -0.0723099661837084, 0.0387715967173523, -0.0607793368610136, 0.110464511693512), logFC.6 = c(0.0848187321362019, -0.299283590551811, 0.0366788808661408, -0.00763280370062748, 0.0145148270035513, -0.0384916970002755, -0.0000335640771631606, 0.0851895375297912, -0.00364050261322463, 0.0602143760128463), logFC.7 = c(0.305256444042024, -0.274308408751318, 0.0977066795857243, -0.0265659018074027, 0.136348613124811, -0.0938364533000299, -0.143634179166262, 0.139913812601005, 0.268708965044232, 0.133427360632365), logFC.8 = c(0.12744808339884, -0.285015311267508, 0.0459140048745496, -0.00976012971218515, 0.13292412700208, -0.184687147498946, -0.0411558715447517, 0.165717944056239, 0.323358546432839, 0.0502386767987279), logFC.9 = c(0.286824598926274, 0.095530985319937, 0.101370835445593, 0.0352336819150421, 0.0573659992830985, -0.0739779010955875, 0.00466993628480923, 0.0486643748696862, 0.0322601740536419, 0.0873158516027886))
This is the only test sample, in real data, each group has 1500-2000 values so if dot merging over each other is not an issue.
Thank you for your kind help. waiting to hear from you peoples
library(tidyverse)
df1 %>%
pivot_longer(-Ensembl_ID) %>%
group_by(name) %>%
mutate(grp_avg = mean(value)) %>%
ungroup() %>% # EDIT -- sort should happen outside groups
mutate(name = fct_reorder(name, grp_avg)) %>%
ggplot(aes(Ensembl_ID, y = value, color = name)) +
ggbeeswarm::geom_beeswarm() +
scale_color_brewer() +
theme(axis.text.x = element_text(angle = 20))
bring your data in long format
define a mid point mean
use geom_jitter to account for overlaying points
map the color and specify scale_color_gradient2
library(tidyverse)
df <- df1 %>%
pivot_longer(
cols = -Ensembl_ID
)
mid <- mean(df$value)
ggplot(df, aes(x = factor(Ensembl_ID), y = value, color=value)) +
geom_jitter(size=4,
position = position_jitter(width = 0.2, height = 0.2)) +
#scale_color_gradient(low="blue", high="red")
scale_color_gradient2(midpoint=mid, low="blue", mid="grey",
high="red", space ="Lab" )+
theme_classic()
Related
I have the follow lines of code:
ggplot() +
geom_line(data=TS_SimHeads_HOBS_final, aes(x=as.Date(Date), y=BH2672), color='red') +
geom_point(data=Hydro_dates_wellData_2014_2018, aes(x=as.Date(Date), y=BH2672), color='red') +
geom_line(data=TS_SimHeads_HOBS_final, aes(x=as.Date(Date), y=BH3025), color='green') +
geom_point(data=Hydro_dates_wellData_2014_2018, aes(x=as.Date(Date), y=BH3025), color='green') +
xlab("Date") + ylab("Head")
#theme_bw()
which generate the following plot:
What I am trying to do, unsuccessfully, is to include legends only for the lines (points are the experimental data and lines the simulated ones). Some data for reproduction purposes:
Date BH2672 BH278 BH2978 BH2987 BH3025 BH312 BH3963 BH3962 BH3957
2014-02-19 31.28400 78.86755 5.671027 39.48419 53.60201 44.29516 69.23685 61.70843 56.13871
2014-02-20 30.76656 78.87344 5.656940 39.49012 53.56489 44.50679 69.50910 61.70638 56.09621
2014-02-21 30.43226 78.88097 5.642136 39.49902 53.56041 44.65761 69.65709 61.70126 56.04346
2014-02-22 30.16532 78.88979 5.643818 39.51101 53.56065 44.78333 69.75621 61.69643 55.99459
2014-02-23 29.93577 78.89954 5.650873 39.52544 53.55970 44.89429 69.82983 61.69332 55.95241
2014-02-24 29.73162 78.90991 5.658991 39.54147 53.55682 44.99520 69.88845 61.69236 55.91639
As is quite often the case you first have to convert both of your datasets to long or tidy format using e.g. tidyr::pivot_longer which will result in a new column with the variable names as categories which could then be mapped on the color aes. Doing so will automatically create a legend and also allows to simplify your code. And if you want only the lines to appear in the legend then you could add show.legend=FALSE to geom_point. Finally you can set your desired colors via scale_color_manual.
As you provided only one dataset I used this for both datasets which however shouldn't matter. Also, to make my life a bit easier I have put the datasets in an named list:
library(dplyr, warn = FALSE)
library(tidyr)
library(ggplot2)
data_list <- list(data = Hydro_dates_wellData_2014_2018, sim = TS_SimHeads_HOBS_final) %>%
lapply(function(x) {
x %>%
select(Date, BH2672, BH3025) %>%
mutate(Date = as.Date(Date)) %>%
tidyr::pivot_longer(-Date)
})
ggplot() +
geom_line(data=data_list$sim, aes(x=Date, y=value, color = name)) +
geom_point(data=data_list$data, aes(x=Date, y=value, color = name), show.legend = FALSE) +
scale_color_manual(values = c(BH2672 = "red", BH3025 = "green")) +
labs(x = "Date", y = "Head")
DATA
TS_SimHeads_HOBS_final <- structure(list(Date = c(
"2014-02-19", "2014-02-20", "2014-02-21",
"2014-02-22", "2014-02-23", "2014-02-24"
), BH2672 = c(
31.284,
30.76656, 30.43226, 30.16532, 29.93577, 29.73162
), BH278 = c(
78.86755,
78.87344, 78.88097, 78.88979, 78.89954, 78.90991
), BH2978 = c(
5.671027,
5.65694, 5.642136, 5.643818, 5.650873, 5.658991
), BH2987 = c(
39.48419,
39.49012, 39.49902, 39.51101, 39.52544, 39.54147
), BH3025 = c(
53.60201,
53.56489, 53.56041, 53.56065, 53.5597, 53.55682
), BH312 = c(
44.29516,
44.50679, 44.65761, 44.78333, 44.89429, 44.9952
), BH3963 = c(
69.23685,
69.5091, 69.65709, 69.75621, 69.82983, 69.88845
), BH3962 = c(
61.70843,
61.70638, 61.70126, 61.69643, 61.69332, 61.69236
), BH3957 = c(
56.13871,
56.09621, 56.04346, 55.99459, 55.95241, 55.91639
)), class = "data.frame", row.names = c(
NA,
-6L
))
Hydro_dates_wellData_2014_2018 <- TS_SimHeads_HOBS_final
For the time series plot which is composed by two subplots:
library(tidyverse)
library(lubridate)
library(feasts)
library(tsibble)
library(gghighlight)
df %>%
mutate(date = as.Date(date, origin = "1899-12-30")) %>%
mutate(year=as.numeric(year(date))) %>%
pivot_longer(`food_index`:`energy_index`) %>%
mutate(date=yearmonth(date)) %>%
as_tsibble(index=date, key=name) %>%
gg_season(value, alpha=1) +
geom_line(size=0.8, alpha=0.8) +
geom_point(size=2, alpha=1)
Out:
Let's say if the current year is 2022, I wanna to plot the line of that year with alpha=1, other years' lines with smaller alpha, ie., alpha=0.3.
How could I do that? Thanks for your helps at advance.
Data:
df <- structure(list(date = c(42766, 42794, 42825, 42855, 42886, 42916,
42947, 42978, 43008, 43039, 43069, 43100, 43131, 43159, 43190,
43220, 43251, 43281, 43312, 43343, 43373, 43404, 43434, 43465,
43496, 43524, 43555, 43585, 43616, 43646, 43677, 43708, 43738,
43769, 43799, 43830, 43861, 43890, 43921, 43951, 43982, 44012,
44043, 44074, 44104, 44135, 44165, 44196, 44227, 44255, 44286,
44316, 44347, 44377, 44408, 44439, 44469, 44500, 44530, 44561
), food_index = c(58.53, 61.23, 55.32, 55.34, 61.73, 56.91, 54.27,
59.08, 60.11, 66.01, 60.11, 63.41, 69.8, 72.45, 81.11, 89.64,
88.64, 88.62, 98.27, 111.11, 129.39, 140.14, 143.44, 169.21,
177.39, 163.88, 135.07, 151.28, 172.81, 143.82, 162.13, 172.22,
176.67, 179.3, 157.27, 169.12, 192.51, 194.2, 179.4, 169.1, 193.17,
174.92, 181.92, 188.41, 192.14, 203.41, 194.19, 174.3, 174.86,
182.33, 182.82, 185.36, 192.41, 195.59, 202.6, 201.51, 225.01,
243.78, 270.67, 304.57), energy_index = c(127.36, 119.87, 120.96,
112.09, 112.19, 109.24, 109.56, 106.89, 109.35, 108.35, 112.39,
117.77, 119.52, 122.24, 120.91, 125.41, 129.72, 135.25, 139.33,
148.6, 169.62, 184.23, 204.38, 198.55, 189.29, 202.47, 220.23,
240.67, 263.12, 249.74, 240.84, 243.42, 261.2, 256.76, 258.69,
277.98, 289.63, 293.46, 310.81, 318.68, 310.04, 302.17, 298.62,
260.92, 269.29, 258.84, 241.68, 224.18, 216.36, 226.57, 235.98,
253.86, 267.37, 261.99, 273.37, 280.91, 291.84, 297.88, 292.78,
289.79)), row.names = c(NA, 60L), class = "data.frame")
You could achieve this by creating a boolean variable that detects the year you would like to highlight and then passing that as the alpha aesthetic inside your plot:
df %>%
mutate(date = as.Date(date, origin = "1899-12-30")) %>%
mutate(year=as.numeric(year(date))) %>%
pivot_longer(`food_index`:`energy_index`) %>%
mutate(date=yearmonth(date),
highlight = ifelse(year == "2021", T, F)) %>%
as_tsibble(index=date, key=name) %>%
gg_season(value, alpha = 0.2) +
geom_line(aes(alpha = highlight),
size=0.8) +
geom_point(aes(alpha = highlight),
size=2) +
scale_alpha_manual(values = c(0.2, 1)) +
guides(alpha = "none") +
theme_bw()
I have the following matrix :
df1 <- read.table(text= 'Sample Al2O3_D1 CaO_D1 Fe2O3_D1 K2O_D1 SiO2_D1 TiO2_D1 Al2O3_D1b0 CaO_D1b0 Fe2O3_D1b0 K2O_D1b0 SiO2_D1b0 TiO2_D1b0 Al2O3_D2 CaO_D2 Fe2O3_D2 K2O_D2 SiO2_D2 TiO2_D2 Al2O3_D2b0 CaO_D2b0 Fe2O3_D2b0 K2O_D2b0 SiO2_D2b0 TiO2_D2b0
BIL1 18.527771 18.256632 6.56251 8.342669 4.374867 0.5842755 17.7422245 23.987872 6.238913 6.780187 5.211082 0.1722543 14.524362 11.540785027 6.567724 11.59751 2.716793 0.148641 21.5237007 32.4114021 6.802355 12.696409 2.418464 0.9386197
Ha11 4.397232 12.888497 34.33309 52.961988 11.131223 45.1434655 15.0384636 12.980365 30.093978 54.002291 3.393567 38.6145158 12.323475 17.028706310 38.690775 49.06582 20.080664 45.619040 3.5297936 16.5442327 41.180170 47.440273 24.276975 44.3668208
Ha137 103.000052 2.212967 51.25095 306.746410 121.806659 63.6579611 212.8800243 2.165292 18.769017 364.236920 62.013419 21.9816389 106.235891 1.269353476 5.057160 287.15363 55.081696 58.204002 161.4670946 1.2884466 34.972412 298.504372 104.189560 31.8284467
Ha171 90.591557 1.569013 67.93309 232.845227 127.158291 75.5793762 254.3307398 1.513897 27.405290 310.109817 42.146396 27.6889288 79.012056 0.145186508 3.886563 222.66687 7.391477 68.673164 191.7846982 0.2314552 42.085088 246.272719 80.267907 36.9812088
Ha21 7.097864 17.014437 45.15964 69.600986 13.324667 55.9935294 25.9518750 17.074982 40.215454 74.263048 2.894586 46.7076167 15.344770 20.336227579 46.986192 62.41907 18.820986 55.880410 10.5328447 19.9025608 50.333141 60.448031 25.283637 52.3899333
Ha28a 14.276762 10.891896 36.92640 93.706836 22.456851 53.3221147 33.2916429 10.929970 31.081099 98.541650 11.519390 43.0020990 23.122859 13.698527956 38.803813 85.77801 28.915269 53.137565 17.2874734 13.2874359 42.799188 83.485379 35.607017 49.1309903
Ha45 8.755274 16.194970 23.72806 24.662651 10.998789 33.8922508 9.9868429 16.441356 22.187580 23.786921 6.279046 30.1799502 14.205216 22.219296703 30.537651 24.31487 22.675330 34.744338 4.7672324 21.8993271 30.730442 23.807887 24.471693 35.2918163
Ha56 2.519377 19.291306 17.78027 14.689078 1.340254 26.7134525 0.3009044 19.692186 16.525441 13.056810 1.866104 25.1075378 5.078764 25.877687826 24.668074 16.58434 12.172266 27.542356 0.9123158 25.9770408 24.642464 16.954296 13.036683 28.7758434
Ha78a 16.630011 10.518633 36.20753 65.697116 18.732162 49.0261161 31.7415436 10.576057 31.740250 68.840011 9.396106 41.7404661 25.679895 13.894132835 40.025832 59.56926 26.868566 49.336063 17.2903714 13.4394515 42.754641 57.575556 32.258478 47.4607437
Ha91 47.531258 6.963291 44.16573 120.753235 57.927836 57.5642663 92.4665900 6.947703 35.101718 135.376499 34.582463 39.3617521 56.780675 7.732582039 39.817328 109.13196 49.911602 56.081569 64.6430022 7.5310163 47.001601 108.587613 66.871031 46.5091579
L110 32.616500 6.162218 48.84669 204.508488 51.576478 64.5594220 100.1659655 6.133002 32.384991 232.810812 20.687400 40.3856100 35.670427 6.239853155 31.080776 188.34130 26.920909 61.901493 67.6463355 6.1250955 45.472784 190.557631 51.085145 47.6973747
L28d 213.963550 4.226734 91.60353 263.427318 250.950733 98.6845750 638.5723017 4.165268 36.270331 529.076984 70.311398 0.0038225 154.434579 2.280303654 3.936042 300.61284 42.674167 82.568998 501.6810607 2.4456430 49.319737 419.089754 116.983089 13.1846725
SARM41 9.206072 8.542555 16.87283 1.652141 6.469613 4.8801782 9.9053176 15.636966 17.675264 3.357829 7.712771 5.6633793 5.816894 1.068768333 9.466277 6.61795 1.658162 3.989925 12.5027313 25.0161949 9.891560 7.313323 1.511116 2.3360493', header=T, stringsAsFactors = F)
These are chemical analysis for different samples obtained with different techniques (techniques are D1, D1b0, D2, D2b0). So for each sample, I have an analysis of different elements (e.g.) Al2O3 made with 4 techniques (Al2O3_D1, Al2O3_D1b0, Al2O3_D2, Al2O3_D2bO)
I want to make boxplot of the variations associated with each element and each technique, with the mean value obtained for each element and each technique in a facet_wrap, so I made the following code:
df1 %>%
pivot_longer(cols = -Sample) %>%
separate(name, c("key","number"), sep = "_") %>%
group_by(number) %>%
ggplot(aes(x=number, y=value, fill = number)) +
geom_boxplot() +
scale_fill_brewer(palette = "RdBu") +
scale_y_log10(limits = c(0.01, NA)) +
theme(legend.position="bottom") +
stat_summary(aes(y = value, label = round(..y.., 2)),
fun = mean, geom = "text", vjust=10,
position = position_dodge(0.9)) +
facet_wrap(key~., ncol=2)
They are two problems with this code:
it is giving me a mean value for something else than what I want. I should have for Al2O3: D1 135.32, D1b0 390.2, D2 100.29, D2b0 307.55
Instead, I have D1 1.3, D1b0 1.53, D2 1.4, D2b0 1.38
I would like all the mean values to be aligned on the same line (e.g. along 1e-1 line) on top of their respective technique but they are not aligned.
Thanks a lot for your help, Anne-Christine
You can use something like the following
df1 %>%
pivot_longer(cols = -Sample) %>%
separate(name, c("key","number"), sep = "_") %>%
group_by(number) %>%
ggplot(aes(x=number, y=value, fill = number)) +
geom_boxplot() +
scale_fill_brewer(palette = "RdBu") +
theme(legend.position="bottom") +
stat_summary(aes(y = value, label = round(..y.., 2)), fun = mean, geom = "text",colour="darkblue") +
facet_wrap(key~., ncol=2, scales = "free")
I will just piggyback off Bappa's answer and your updated code (I don't have enough rep to just comment)
df1 %>%
pivot_longer(cols = -Sample) %>%
separate(name, c("key","number"), sep = "_") %>%
group_by(key, number) %>%
mutate(mean = mean(value, na.rm = T)) %>%
ggplot(aes(x=number, y=value, fill = number)) +
geom_boxplot() +
scale_fill_brewer(palette = "RdBu") +
scale_y_log10() +
geom_text(aes(label = round(mean, 2), y = 1e-01)) +
facet_wrap(key~., ncol=2)
By getting the mean value before creating the plot and log transforming, you should be able to get around the issue with the means you had. The numbers align with those Bappa displayed.
Using geom_text, you can position the labels wherever you like.
I'm sure this is stupidly simple but it's been some time since I've used R, and I have never made a barchart with ggplot.
I have the following averages from a larger dataset:
> mean_gc
PVC1 PVC2 PVC3 PVC4 PVC5 PVC6 PVC7 PVC8 PVC9 PVC10 PVC11
0.4019026 0.4479259 0.4494118 0.4729437 0.4800556 0.4492290 0.4905295 0.4457566 0.4271259 0.4850341 0.4369965
PVC12 PVC13 PVC14 PVC15 PVC16
0.4064052 0.3743776 0.3603853 0.3965469 0.3654610
My end goal is to plot a bar chart (since each "PVC#" is discrete), and fit a step-function across it in R to try and find subtle 'breakpoints' - but that's a problem for later...
The only way I've been able to achieve a barplot from this is using barplot which creates the graph below.
Which is fine, but it's ugly compared to ggplot.
I've tried setting the above data as a dataframe both with the PVC labels in the dataframe, and as rownames - but I just can't get the syntax right and I'm at my wits end!
What am I missing?
EDIT FOR CLARITY ON DATAFRAMES
The above was just the printed output in R (not the best way to show it - my apologies). I have the data in the following (column based format):
mean_gc
PVC1 0.4019026
PVC2 0.4479259
PVC3 0.4494118
PVC4 0.4729437
PVC5 0.4800556
PVC6 0.4492290
PVC7 0.4905295
PVC8 0.4457566
PVC9 0.4271259
PVC10 0.4850341
PVC11 0.4369965
PVC12 0.4064052
PVC13 0.3743776
PVC14 0.3603853
PVC15 0.3965469
PVC16 0.3654610
Where PVC# are the row.names. I also have the same dataset where the row.names are present as the first column, in case that is required (but I suspect not).
You have to melt() your data before you can use ggplot2, because it assumes a tidy data structure.
library(reshape2)
library(ggplot2)
ggplot(melt(df), aes(variable, value)) +
geom_bar(stat = "identity")
Data
df <- structure(list(PVC1 = 0.4019026, PVC2 = 0.4479259, PVC3 = 0.4494118,
PVC4 = 0.4729437, PVC5 = 0.4800556, PVC6 = 0.449229, PVC7 = 0.4905295,
PVC8 = 0.4457566, PVC9 = 0.4271259, PVC10 = 0.4850341, PVC11 = 0.4369965,
PVC12 = 0.4064052, PVC13 = 0.3743776, PVC14 = 0.3603853,
PVC15 = 0.3965469, PVC16 = 0.365461), .Names = c("PVC1",
"PVC2", "PVC3", "PVC4", "PVC5", "PVC6", "PVC7", "PVC8", "PVC9",
"PVC10", "PVC11", "PVC12", "PVC13", "PVC14", "PVC15", "PVC16"
), class = "data.frame", row.names = c(NA, -1L))
I have a data frame that comes from a lm subset composed of the intercept (ordenada) and the slope (velocidad1) calculated for each subject.
A
UT1 UT2 UT3 UT4
ordenada 1213.8 2634.8 3.760000e+02 -11080.8
velocidad1 1.5 -2.5 6.615954e-14 20.0
UT5 UT6 UT7
ordenada 1711.8 1.739000e+03 1.800000e+01
velocidad1 -2.5 5.039544e-14 -9.154345e-16
UT8 UT9 UT10 UT11 UT12
ordenada 5659.2 -2791 3422.6 418.2 2802.2
velocidad1 -6.0 5 -1.0 -0.5 -1.5
UT13 UT14 TR1 TR2
ordenada 2.832000e+03 -411.2 -15722.0 -1105.4
velocidad1 1.405114e-13 3.5 25.5 25.0
TR3 TR4 TR5 TR6
ordenada 1.14600e+03 299.6 1943.4 6.840000e+02
velocidad1 -5.11402e-14 2.0 -2.5 6.479414e-14
TR7 TR8 TR9 TR10
ordenada 354.8 1.317000e+03 33284.6 -3742.6
velocidad1 1.0 -3.475548e-14 -52.0 8.0
TR11 TR12 TR13
ordenada 7.400000e+02 2205.4 -4542.6
velocidad1 -8.018585e-14 -2.5 8.0
TR14
ordenada 5.880000e+02
velocidad1 -4.406498e-14
dput(A)
structure(list(UT1 = c(1213.79999999971, 1.50000000000047), UT2 = c(2634.80000000021,
-2.50000000000033), UT3 = c(375.999999999959, 6.61595351840473e-14
), UT4 = c(-11080.8000000008, 20.0000000000013), UT5 = c(1711.80000000007,
-2.50000000000012), UT6 = c(1738.99999999997, 5.03954433109254e-14
), UT7 = c(18.0000000000006, -9.15434469010036e-16), UT8 = c(5659.20000000026,
-6.00000000000041), UT9 = c(-2791.00000000024, 5.00000000000039
), UT10 = c(3422.59999999968, -0.99999999999948), UT11 = c(418.199999999958,
-0.499999999999932), UT12 = c(2802.20000000017, -1.50000000000028
), UT13 = c(2831.99999999991, 1.40511433073812e-13), UT14 = c(-411.200000000294,
3.50000000000048), TR1 = c(-15722.0000000017, 25.5000000000028
), TR2 = c(-1105.40000000264, 25.0000000000043), TR3 = c(1146.00000000003,
-5.11402035568996e-14), TR4 = c(299.599999999803, 2.00000000000032
), TR5 = c(1943.40000000013, -2.50000000000021), TR6 = c(683.99999999996,
6.47941413997612e-14), TR7 = c(354.800000000011, 0.999999999999982
), TR8 = c(1317.00000000002, -3.47554781454658e-14), TR9 = c(33284.6000000025,
-52.000000000004), TR10 = c(-3742.60000000058, 8.00000000000094
), TR11 = c(740.00000000005, -8.0185853149896e-14), TR12 = c(2205.40000000021,
-2.50000000000034), TR13 = c(-4542.60000000042, 8.00000000000067
), TR14 = c(588.000000000027, -4.40649812201441e-14)), .Names = c("UT1",
"UT2", "UT3", "UT4", "UT5", "UT6", "UT7", "UT8", "UT9", "UT10",
"UT11", "UT12", "UT13", "UT14", "TR1", "TR2", "TR3", "TR4", "TR5",
"TR6", "TR7", "TR8", "TR9", "TR10", "TR11", "TR12", "TR13", "TR14"
), row.names = c("ordenada", "velocidad1"), class = "data.frame")
My goal is to get a barplot of the data in second row ( A[2,] ) splitting by group (UT which contains UT1,UT2... and TR) in the same graph. I am trying to do some ggplot but keep failing over and over again. I get no layers in plot error or margin error in base graphics.
The output should look like this
I KNOW the answer is in the reshape package but I wish there's another way to do that.
Thank you in advance.
Using base graphics:
# convert the one-row data frame to a two-row matrix
m <- matrix(unlist(df[2, ]), nrow = 2, byrow = TRUE)
# plot
barplot(m, beside = TRUE, col = c("blue", "red"), names.arg = seq_len(ncol(m)))
Possibly add a legend:
legend("topright", legend = c("UT", "TR"), fill = c("blue", "red"))
EDIT: Not using reshape per request in comments
library(ggplot2)
plot_data <- data.frame(names(A), t(A[2,]))
names(plot_data) <- c("variable", "value")
plot_data$group <- grepl("^TR", plot_data$variable)
plot_data$variable <- gsub("[^0-9]", "", as.character(plot_data$variable))
plot_data$variable <- factor(plot_data$variable,
unique(sort(as.numeric(plot_data$variable))))
p <- ggplot(aes(y = value, x = variable, fill = group), data = plot_data)
p + geom_bar(stat = "identity", position = "dodge")
Here is another option that incorporates your complete dataset. Not sure if this is usefull for you.
I've used reshape2, it's actually easier. You just have to melt(yourdataframe), for your particular case there is no need to specify anything else in the melt function arguments.
require("ggplot2")
require("reshape2")
A <- df
df1 <- melt(df[1,])
df1$origen <- "ORDENADA"
df2 <- melt(df[2,])
df2$origen <- "VELOCIDAD"
identical(df1$variable,df2$variable)
df3 <- rbind(df1,df2)
df3$group <- ifelse(grepl("^TR", df3$variable) == TRUE, "TR", "UT")
df3$vble <- gsub("[^0-9]", "", as.character(df3$variable))
df3$vble <- factor(df3$vble, levels = as.numeric(unique(df3$vble)))
ggplot(aes(y = value, x = vble, fill = group), data = df3) +
geom_bar(stat = "identity", position = "dodge") +
facet_grid(origen ~ ., scales = "free")
Using Functions
prepare <- function(data){
data1 <- melt(data[1,])
data1$origen <- "ORDENADA"
data2 <- melt(data[2,])
data2$origen <- "VELOCIDAD"
identical(data1$variable,data2$variable)
data3 <- rbind(data1,data2)
data3$group <- ifelse(grepl("^TR", data3$variable) == TRUE, "TR", "UT")
data3$vble <- gsub("[^0-9]", "", as.character(data3$variable))
data3$vble <- factor(data3$vble, levels = as.numeric(unique(data3$vble)))
return(data3)
}
prepare(df)
#This would work, but is a bit manual for many plots:
ggplot(aes(y = value, x = vble, fill = group), data = prepare(df)) +
geom_bar(stat = "identity", position = "dodge") +
facet_grid(origen ~ ., scales = "free")
plot_fun <- function(data){
p <- ggplot(data, aes_string(x = "vble", y = "value", fill = "group"))
p <- p + geom_bar(stat = "identity", position = "dodge")
p <- p + facet_grid(origen ~ ., scales = "free")
suppressWarnings(print(p))
}
plot_fun(prepare(df))
I guess you could loop in order to plot several data frames using the same plot function.
I guess you could probably addapt it more to your needs, but this can get you started