How to add gradient to button in Xamarin.Forms using CSS - xamarin.forms

Add gradient to button CSS in Xamarin.Forms.
I have idea of doing it using custom renderer, but is there a way to do it using CSS.

Please find Below link.
I'm pretty sure this is exactly what you want
Link
Not sure if you researched anything before asking this
Here's some links on this topic after one search.
1
https://github.com/mgierlasinski/MagicGradients
Gradients in CSS
If you want to use Magic Gradient for 100% and don’t want to import any CSS files into your project, you may try πŸ₯ β€” Inline CSS 🀯
What? Inline CSS? What the ****? β€” This means that you have some string inline in XAML or in resources under some key, that string has special prefix and postfix in his value and parsed by Xamarin.Forms in the same way as other CSS from files.
<magic:GradientView VerticalOptions="FillAndExpand">
<magic:GradientView.GradientSource>
<magic:CssGradientSource>
<x:String>
<![CDATA[
linear-gradient(242deg, rgba(195, 195, 195, 0.02) 0%, rgba(195, 195, 195, 0.02) 16.667%,rgba(91, 91, 91, 0.02) 16.667%, rgba(91, 91, 91, 0.02) 33.334%,rgba(230, 230, 230, 0.02) 33.334%, rgba(230, 230, 230, 0.02) 50.001000000000005%,rgba(18, 18, 18, 0.02) 50.001%, rgba(18, 18, 18, 0.02) 66.668%,rgba(163, 163, 163, 0.02) 66.668%, rgba(163, 163, 163, 0.02) 83.33500000000001%,rgba(140, 140, 140, 0.02) 83.335%, rgba(140, 140, 140, 0.02) 100.002%),linear-gradient(152deg, rgba(151, 151, 151, 0.02) 0%, rgba(151, 151, 151, 0.02) 16.667%,rgba(11, 11, 11, 0.02) 16.667%, rgba(11, 11, 11, 0.02) 33.334%,rgba(162, 162, 162, 0.02) 33.334%, rgba(162, 162, 162, 0.02) 50.001000000000005%,rgba(171, 171, 171, 0.02) 50.001%, rgba(171, 171, 171, 0.02) 66.668%,rgba(119, 119, 119, 0.02) 66.668%, rgba(119, 119, 119, 0.02) 83.33500000000001%,rgba(106, 106, 106, 0.02) 83.335%, rgba(106, 106, 106, 0.02) 100.002%),linear-gradient(11deg, rgba(245, 245, 245, 0.01) 0%, rgba(245, 245, 245, 0.01) 16.667%,rgba(23, 23, 23, 0.01) 16.667%, rgba(23, 23, 23, 0.01) 33.334%,rgba(96, 96, 96, 0.01) 33.334%, rgba(96, 96, 96, 0.01) 50.001000000000005%,rgba(140, 140, 140, 0.01) 50.001%, rgba(140, 140, 140, 0.01) 66.668%,rgba(120, 120, 120, 0.01) 66.668%, rgba(120, 120, 120, 0.01) 83.33500000000001%,rgba(48, 48, 48, 0.01) 83.335%, rgba(48, 48, 48, 0.01) 100.002%),linear-gradient(27deg, rgba(106, 106, 106, 0.03) 0%, rgba(106, 106, 106, 0.03) 14.286%,rgba(203, 203, 203, 0.03) 14.286%, rgba(203, 203, 203, 0.03) 28.572%,rgba(54, 54, 54, 0.03) 28.572%, rgba(54, 54, 54, 0.03) 42.858%,rgba(75, 75, 75, 0.03) 42.858%, rgba(75, 75, 75, 0.03) 57.144%,rgba(216, 216, 216, 0.03) 57.144%, rgba(216, 216, 216, 0.03) 71.42999999999999%,rgba(39, 39, 39, 0.03) 71.43%, rgba(39, 39, 39, 0.03) 85.71600000000001%,rgba(246, 246, 246, 0.03) 85.716%, rgba(246, 246, 246, 0.03) 100.002%),linear-gradient(317deg, rgba(215, 215, 215, 0.01) 0%, rgba(215, 215, 215, 0.01) 16.667%,rgba(72, 72, 72, 0.01) 16.667%, rgba(72, 72, 72, 0.01) 33.334%,rgba(253, 253, 253, 0.01) 33.334%, rgba(253, 253, 253, 0.01) 50.001000000000005%,rgba(4, 4, 4, 0.01) 50.001%, rgba(4, 4, 4, 0.01) 66.668%,rgba(183, 183, 183, 0.01) 66.668%, rgba(183, 183, 183, 0.01) 83.33500000000001%,rgba(17, 17, 17, 0.01) 83.335%, rgba(17, 17, 17, 0.01) 100.002%),linear-gradient(128deg, rgba(119, 119, 119, 0.03) 0%, rgba(119, 119, 119, 0.03) 12.5%,rgba(91, 91, 91, 0.03) 12.5%, rgba(91, 91, 91, 0.03) 25%,rgba(45, 45, 45, 0.03) 25%, rgba(45, 45, 45, 0.03) 37.5%,rgba(182, 182, 182, 0.03) 37.5%, rgba(182, 182, 182, 0.03) 50%,rgba(243, 243, 243, 0.03) 50%, rgba(243, 243, 243, 0.03) 62.5%,rgba(162, 162, 162, 0.03) 62.5%, rgba(162, 162, 162, 0.03) 75%,rgba(190, 190, 190, 0.03) 75%, rgba(190, 190, 190, 0.03) 87.5%,rgba(148, 148, 148, 0.03) 87.5%, rgba(148, 148, 148, 0.03) 100%),linear-gradient(90deg, rgb(185, 139, 80),rgb(176, 26, 6))
]]>
</x:String>
</magic:CssGradientSource>
</magic:GradientView.GradientSource>
</magic:GradientView>
And if we want to use the same from CSS:
.gradientStyledWithCss {
background: linear-gradient(242deg, rgba(195, 195, 195, 0.02) 0%, rgba(195, 195, 195, 0.02) 16.667%,rgba(91, 91, 91, 0.02) 16.667%, rgba(91, 91, 91, 0.02) 33.334%,rgba(230, 230, 230, 0.02) 33.334%, rgba(230, 230, 230, 0.02) 50.001000000000005%,rgba(18, 18, 18, 0.02) 50.001%, rgba(18, 18, 18, 0.02) 66.668%,rgba(163, 163, 163, 0.02) 66.668%, rgba(163, 163, 163, 0.02) 83.33500000000001%,rgba(140, 140, 140, 0.02) 83.335%, rgba(140, 140, 140, 0.02) 100.002%),linear-gradient(152deg, rgba(151, 151, 151, 0.02) 0%, rgba(151, 151, 151, 0.02) 16.667%,rgba(11, 11, 11, 0.02) 16.667%, rgba(11, 11, 11, 0.02) 33.334%,rgba(162, 162, 162, 0.02) 33.334%, rgba(162, 162, 162, 0.02) 50.001000000000005%,rgba(171, 171, 171, 0.02) 50.001%, rgba(171, 171, 171, 0.02) 66.668%,rgba(119, 119, 119, 0.02) 66.668%, rgba(119, 119, 119, 0.02) 83.33500000000001%,rgba(106, 106, 106, 0.02) 83.335%, rgba(106, 106, 106, 0.02) 100.002%),linear-gradient(11deg, rgba(245, 245, 245, 0.01) 0%, rgba(245, 245, 245, 0.01) 16.667%,rgba(23, 23, 23, 0.01) 16.667%, rgba(23, 23, 23, 0.01) 33.334%,rgba(96, 96, 96, 0.01) 33.334%, rgba(96, 96, 96, 0.01) 50.001000000000005%,rgba(140, 140, 140, 0.01) 50.001%, rgba(140, 140, 140, 0.01) 66.668%,rgba(120, 120, 120, 0.01) 66.668%, rgba(120, 120, 120, 0.01) 83.33500000000001%,rgba(48, 48, 48, 0.01) 83.335%, rgba(48, 48, 48, 0.01) 100.002%),linear-gradient(27deg, rgba(106, 106, 106, 0.03) 0%, rgba(106, 106, 106, 0.03) 14.286%,rgba(203, 203, 203, 0.03) 14.286%, rgba(203, 203, 203, 0.03) 28.572%,rgba(54, 54, 54, 0.03) 28.572%, rgba(54, 54, 54, 0.03) 42.858%,rgba(75, 75, 75, 0.03) 42.858%, rgba(75, 75, 75, 0.03) 57.144%,rgba(216, 216, 216, 0.03) 57.144%, rgba(216, 216, 216, 0.03) 71.42999999999999%,rgba(39, 39, 39, 0.03) 71.43%, rgba(39, 39, 39, 0.03) 85.71600000000001%,rgba(246, 246, 246, 0.03) 85.716%, rgba(246, 246, 246, 0.03) 100.002%),linear-gradient(317deg, rgba(215, 215, 215, 0.01) 0%, rgba(215, 215, 215, 0.01) 16.667%,rgba(72, 72, 72, 0.01) 16.667%, rgba(72, 72, 72, 0.01) 33.334%,rgba(253, 253, 253, 0.01) 33.334%, rgba(253, 253, 253, 0.01) 50.001000000000005%,rgba(4, 4, 4, 0.01) 50.001%, rgba(4, 4, 4, 0.01) 66.668%,rgba(183, 183, 183, 0.01) 66.668%, rgba(183, 183, 183, 0.01) 83.33500000000001%,rgba(17, 17, 17, 0.01) 83.335%, rgba(17, 17, 17, 0.01) 100.002%),linear-gradient(128deg, rgba(119, 119, 119, 0.03) 0%, rgba(119, 119, 119, 0.03) 12.5%,rgba(91, 91, 91, 0.03) 12.5%, rgba(91, 91, 91, 0.03) 25%,rgba(45, 45, 45, 0.03) 25%, rgba(45, 45, 45, 0.03) 37.5%,rgba(182, 182, 182, 0.03) 37.5%, rgba(182, 182, 182, 0.03) 50%,rgba(243, 243, 243, 0.03) 50%, rgba(243, 243, 243, 0.03) 62.5%,rgba(162, 162, 162, 0.03) 62.5%, rgba(162, 162, 162, 0.03) 75%,rgba(190, 190, 190, 0.03) 75%, rgba(190, 190, 190, 0.03) 87.5%,rgba(148, 148, 148, 0.03) 87.5%, rgba(148, 148, 148, 0.03) 100%),linear-gradient(90deg, rgb(185, 139, 80),rgb(176, 26, 6));
}
<ContentPage.Resources>
<StyleSheet Source="../Styles/LinearGradientsPage.css" />
</ContentPage.Resources>
...
<Button StyleClass="gradientStyledWithCss" />

Related

time series equation in R

I have data that looks similar to the following example data and I'm looking for a way to fit an equation that i can use on other data with similar profiles but might be higher or lower.
structure(list(day = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132,
133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145,
146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158,
159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236,
237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
250, 251, 252, 253), Count = c(10, 50, 500, 425, 300, 400, 275,
98, 115, 79, 87, 114, 69, 105, 81, 82, 117, 87, 123, 81, 119,
97, 84, 124, 122, 53, 114, 95, 49, 95, 101, 114, 74, 120, 72,
61, 79, 59, 96, 95, 105, 53, 110, 69, 69, 79, 106, 52, 50, 98,
102, 107, 122, 108, 47, 68, 51, 114, 96, 102, 121, 113, 130,
134, 143, 144, 141, 139, 140, 142, 141, 125, 134, 130, 137, 139,
123, 138, 108, 133, 97, 122, 120, 110, 144, 121, 103, 127, 103,
100, 139, 138, 103, 105, 114, 142, 128, 141, 141, 122, 110, 125,
112, 98, 130, 116, 138, 120, 135, 143, 136, 145, 101, 120, 131,
119, 131, 116, 114, 143, 126, 102, 116, 106, 133, 110, 102, 141,
141, 132, 110, 95, 130, 133, 131, 128, 103, 111, 120, 140, 107,
114, 95, 113, 116, 131, 145, 144, 121, 111, 100, 145, 96, 130,
95, 119, 135, 127, 113, 105, 110, 102, 105, 116, 145, 115, 102,
120, 143, 140, 141, 132, 143, 136, 108, 106, 127, 112, 122, 118,
112, 96, 116, 141, 162, 168, 198, 156, 165, 180, 179, 166, 194,
194, 162, 199, 156, 193, 200, 160, 160, 187, 150, 185, 161, 183,
166, 167, 199, 159, 146, 195, 151, 161, 161, 162, 167, 193, 191,
181, 148, 200, 182, 164, 147, 182, 165, 165, 159, 163, 188, 154,
192, 157, 149, 163, 170, 151, 185, 168, 154, 164, 191, 169, 186,
157, 182, 195, 150, 145, 152, 188, 176)), row.names = c(NA, -253L
), class = c("tbl_df", "tbl", "data.frame"))
The red line is an example of what an equation might look like. Very rough drawing.
I think what you may be looking for is a generalized additive model (GAM), which is often used to model nonlinear data like time. Here I have saved your dput as data and fit it to a GAM below. First, we can load the mgcv package for the GAM fit.
#### Load Library ####
library(mgcv)
Then you fit the GAM. This can be a very complex topic, and I advise reading a lot on this, but essentially you fit the regression in a similar manner you are probably used to if you have done regression in R before. The only difference is what spline terms you add to the regression, or the nonlinear functions that approximate the relationship between x and y. Here I have just fit a cubic regression spline using the s function for the spline, "day" as the variable, and bs = "cr" for the cubic regression spline. I also use REML here, recommended by a lot of GAM experts, to automatically adjust the knots and smoothing parameters. This can be customized a lot, but for simplicity I leave it alone here.
#### Fit GAM ####
fit <- gam(
Count ~ s(day, bs = "cr"),
method = "REML",
data=data
)
The results can be run here:
#### Summary ####
summary(fit)
As seen below. Here you see the intercept is listed like typical regression summaries. Now you have an additional "Approximate significance of smooth terms" section, which lists some useful metrics for your smoothing term. EDF is how curvilinear it is, and Ref.df & F are used for the significance test, seen to the far right. In this case, the smoothing term is significant. There are also many model metrics on the bottom that are worth observing:
Family: gaussian
Link function: identity
Formula:
Count ~ s(day, bs = "cr")
Parametric coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 133.538 2.504 53.32 <2e-16 ***
---
Signif. codes: 0 β€˜***’ 0.001 β€˜**’ 0.01 β€˜*’ 0.05 β€˜.’ 0.1 β€˜ ’ 1
Approximate significance of smooth terms:
edf Ref.df F p-value
s(day) 8.037 8.751 17.93 <2e-16 ***
---
Signif. codes: 0 β€˜***’ 0.001 β€˜**’ 0.01 β€˜*’ 0.05 β€˜.’ 0.1 β€˜ ’ 1
R-sq.(adj) = 0.382 Deviance explained = 40.2%
-REML = 1298.7 Scale est. = 1586.9 n = 253
Technically we can write an equation based off this knowledge, but the difference with GAMs is that each spline fit sets separate coefficients for each part of the nonlinear trend, so its not entirely useful for nonlinear data (some reasons are given here). For example, if I want all of the coefficients for a linear equation, I can run coef(fit) and get this very long list:
(Intercept) s(day).1 s(day).2 s(day).3 s(day).4 s(day).5
133.537549 -83.413590 -54.926693 -35.398280 -38.849985 -41.564495
s(day).6 s(day).7 s(day).8 s(day).9
-38.991790 9.101440 4.764924 24.764163
Plotting the data can be done with the below function and is a much better approximation of the regression fit:
#### Plot Fit ####
plot(fit)
Which shows the data fit with its spline and standard error, along with a rug showing the data points with lines on the x axis. This plotting can be customized a lot too, especially with the gratia package, but I leave it here as is. In any case, the interpretation from the plot is far more clear...counts initially decrease a ton, then rebound slightly, plateau for some time, then rebound again before plateauing again.
Hope that is helpful and I recommend reading a lot on this topic. I have included some links to some really useful primers on the subject below.
Citations
Simpson, 2018: GAMs article. This covers mostly fixed effect versions, which you are probably more likely to use.
Pedersen et al., 2019: GAMMs article. This covers some random effects parts too, which may be difficult to understand unless you know more about mixed models.
This book is also a canonical reference to GAMs that is a lot more comprehensive, but I find it is a difficult read and not the best source for beginners.
I am not sure if i got it right,
are you looking for the code to make this kind of plot?
install tidyverse (a collection of packages) and then -
Add this piece of code:
# your code first and then use the pipe operator '%>%':
%>%
ggplot(aes(x = day, y = Count))+geom_line()

Highlighting the part of a line graph that has the highest slope in R

My dataset contains 2 variables Y and X. Y was measured every 1.0 seconds.
My Data:
dput(Dataexample)
structure(list(X = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133,
134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146,
147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185,
186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198,
199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211,
212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224,
225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237,
238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250,
251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263,
264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276,
277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302,
303, 304, 305, 306), Y = c(71756.2344, 71745.85, 70882.42, 71025.61,
70539.02, 70602.3047, 70811.87, 70514.125, 69998.63, 70531.76,
70424.9141, 70663.51, 70075.375, 69731.0859, 70029.74, 70519.31,
69858.63, 69987.23, 70080.56, 69970.63, 69829.6, 69872.12, 69775.68,
69679.24, 69814.05, 69639.84, 69645.02, 69344.35, 69430.41, 70078.49,
69239.65, 69734.1953, 69736.27, 69549.63, 69506.0859, 69108,
69669.91, 69516.45, 69490.54, 69609.77, 69314.29, 69454.25, 69590.07,
69721.76, 69525.79, 69736.27, 69303.92, 69171.23, 69294.59, 69430.41,
69457.36, 69462.54, 69144.27, 69590.07, 69446.99, 70083.67, 69358.87,
69800.56, 69680.28, 69332.95, 69723.83, 69942.63, 69772.56, 69969.59,
69808.86, 70043.23, 70208.13, 70077.45, 69856.56, 70423.875,
69490.54, 69984.12, 70175.98, 70192.58, 70279.7, 70480.93, 70594,
70792.16, 70234.06, 70165.61, 70249.62, 70564.95, 70403.13, 70444.625,
70426.99, 69907.375, 70327.4141, 70686.3359, 70473.67, 71031.83,
70864.78, 70710.1953, 70691.52, 70703.97, 70826.39, 70708.12,
70595.04, 70946.75, 71319.27, 70977.875, 70475.74, 70612.68,
70680.11, 70527.61, 70461.22, 70877.2344, 70631.35, 70723.68,
70677, 70433.21, 70306.6641, 71246.63, 70375.125, 70416.62, 70150.0547,
70733.0156, 70583.63, 70866.86, 70580.5156, 70433.21, 70377.2,
70114.79, 70347.12, 70613.71, 70576.37, 70599.19, 70407.28, 70581.5547,
70650.02, 71122.11, 70909.4, 70694.63, 71076.45, 70650.02, 71133.52,
70810.83, 71240.41, 70630.31, 71144.94, 71493.63, 71117.95, 71374.28,
71143.9, 70805.64, 71349.375, 71208.2344, 71322.39, 71727.1641,
71060.88, 71546.56, 71569.4, 70984.1, 72032.37, 71573.55, 71787.375,
71469.76, 71398.15, 71683.57, 71709.52, 71637.9, 71556.9453,
71870.4141, 71612.99, 71953.47, 71515.43, 71315.125, 72007.4453,
72021.9844, 71549.68, 72001.22, 71359.75, 71775.95, 72327.23,
71949.31, 71844.47, 71857.96, 72128.9141, 72147.6, 71501.94,
72268.05, 72104, 72217.1641, 72253.51, 72198.48, 72908.78, 72084.27,
72653.29, 72431.06, 72858.92, 72512.0547, 72632.5156, 72700.02,
72335.53, 72713.52, 73065.62, 72818.42, 73004.3359, 72458.06,
73436.48, 73231.82, 73002.26, 73313.89, 73213.125, 72980.4453,
72948.25, 73106.13, 72931.625, 73409.47, 73057.31, 73141.4453,
73218.32, 73216.24, 73273.375, 73701.42, 73486.35, 72574.37,
73229.74, 73576.74, 73195.46, 73697.2656, 73115.48, 73065.62,
73062.5, 73111.32, 73988.23, 73619.3359, 73874.95, 73683.76,
73674.41, 73550.7656, 74166.9844, 73875.99, 74013.17, 74092.16,
73872.875, 74015.25, 73984.07, 73911.33, 73606.87, 74082.8, 73866.64,
74550.53, 74271.95, 73980.95, 74502.71, 74901.92, 74753.25, 74310.4141,
75178.51, 74748.05, 74756.37, 75194.1, 74797.95, 75531.0547,
75549.77, 75293.94, 75378.17, 75457.21, 75676.67, 76087.56, 76141.6641,
76008.5, 76241.55, 76585.96, 76091.73, 76880.4844, 76898.18,
77005.38, 77080.32, 77548.78, 77337.4453, 77000.18, 77448.8359,
76997.0547, 77314.54, 77919.47, 77185.46, 78127.75, 77464.45,
78349.59, 77824.71, 77465.49, 77818.46, 78140.25, 78547.51, 77850.74,
78236.06, 78341.2656, 78104.8359, 78464.17, 77888.23, 78392.3,
78686.0547, 78149.625, 78623.5547, 78672.5156, 78810.03, 78498.55,
78652.72, 78717.31, 78831.91, 78882.96, 78715.23, 78499.5859,
78892.3359, 78372.51)), row.names = c(NA, -306L), class = c("tbl_df",
"tbl", "data.frame"))
I have used ggplot to plot the data and used a loop to calculate the average slope within a moving 60-second-window for the entire duration of the dataset to find the 60 consecutive seconds where the slope is greatest.
Code:
library(readr)
library(ggplot2)
Dataexample<- read_csv("HF-6.csv", skip = 3)
Dataexample<- head(Dataexample, -1)
Dataexample$X <- as.numeric(Dataexample$X)
df <- data.frame(Dataexample)
ggplot(data=df, aes(x=X, y=Y, group=1)) +
geom_line()
slopes <- rep(NA, nrow(Dataexample)-59)
for( i in 1:length(slopes)){
slopes[i] <- lm(Y ~ X, data=Dataexample[i:(i+59), ])$coefficients[2]
}
print(slopes)
which.max(slopes)
max(slopes)
My questions is how can I then take the results of my loop that show the consecutive 60 seconds where the slope is highest and change the color of the line in the plot during those 60 seconds to highlight where slope is greatest.
This should work:
maxslope_ind <- which.max(slope)
Dataexample$highlight <- ifelse(Dataexample$X %in% maxslope_ind:(maxslope_ind+59), 1, 0)
library(ggplot2)
ggplot(data=Dataexample, aes(x=X, y=Y, group=1)) +
geom_line(aes(colour=as.factor(highlight)), show.legend=FALSE) +
scale_colour_manual(values=c("black", "red"))

How to write a list into a file in R?

I need help in order to print a list into a file.
Here is the list:
list1<- list(c(1, 2, 3, 4, 5, 6, 14, 15, 28, 29, 30, 35, 36, 49, 50,
73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103,
104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
130, 131, 132, 133, 134, 150, 162, 163, 167, 172, 177, 199, 200,
201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213,
214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226,
227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251))
list2<-list(c(12,367,78))
and the idea is simply to write that list into a file such as file1
The list needs to be in one line between two () and with a content juste before such as (because I will write several list one after each other):
Expected outputfile:
Content_before=(1, 2, 3, 4, 5, 6, 14, 15, 28, 29, 30, 35, 36, 49, 50,
73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103,
104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
130, 131, 132, 133, 134, 150, 162, 163, 167, 172, 177, 199, 200,
201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213,
214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226,
227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251)
Content_before2=(12,367,78)
So far, I tried:
sink("File1)
cat(paste0("Content_before1=",list1))
cat("\n")
cat(paste0("Content_before2=",list2))
sink()
(this example includes two lists)
We can use cat with sprintf
cat(sprintf('Content_before = (%s)\nContent_before2=(%s)',
toString(list1[[1]]), toString(list2[[1]])), '\n')
#Content_before = (1, 2, 3, 4, 5, 6, 14, 15, 28, 29, 30, 35, 36, 49, 50, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 150, 162, 163, 167, 172, 177, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251)
#Content_before2=(12, 367, 78)
Inorder to write to a file, specify the file
cat(sprintf('Content_before = (%s)\nContent_before2=(%s)',
toString(list1[[1]]), toString(list2[[1]])), file = 'file1.txt', '\n')

Return column value based on a vectors of row.names in a data.frame [duplicate]

This question already has an answer here:
extract values from a data frame based on a vector of row numbers in R
(1 answer)
Closed 2 years ago.
I have a sample data.frame below (subset of a very large cyclic database)
> dput(try)
structure(list(Actuator.Force = c(-402.57388, -400.83463, -402.72595,
-404.24283, -404.07663, -403.83575, -407.55435, -418.7684, -435.86246,
-462.38239, -504.09146, -558.40039, -618.46674, -681.58704, -748.87347,
-814.95032, -880.57739, -946.11627, -1012.9043, -1075.2557, -1141.4972,
-1209.1968, -1272.8707, -1336.021, -1400.5078, -1465.5786, -1528.6499,
-1589.5626, -1654.6541, -1717.825, -1780.0903, -1839.9329, -1902.9841,
-1964.1945, -2025.569, -2085.9578, -2148.239, -2207.5295, -2267.5806,
-2328.6467, -2388.4958, -2447.5298, -2506.7534, -2567.687, -2625.7661,
-2682.866, -2741.3511, -2802.1934, -2858.2546, -2915.1028, -2972.7683,
-3030.8093, -3089.2439, -3145.5701, -3199.8442, -3259.2087, -3315.8582,
-3371.958, -3426.5596, -3484.3855, -3541.2642, -3595.3362, -3650.0208,
-3708.3748, -3763.8076, -3820.0623, -3875.3044, -3932.9504, -3989.6238,
-4047.5957, -4104.8169, -4164.8237, -4223.5444, -4283.3813, -4341.3989,
-4403.166, -4462.1479, -4522.5728, -4584.0186, -4644.7656, -4704.3525,
-4762.6826, -4821.8706, -4878.8818, -4924.1021, -4959.0415, -4985.9517,
-5005.4531, -5017.8027, -5026.0757, -5032.3428, -5036.8042, -5038.9292,
-5039.5361, -5043.021, -5043.0981, -5043.0415, -5042.627, -5014.4199,
-4853.5854, -4566.9771, -4198.7612, -3774.5527, -3317.6958, -2847.5229,
-2364.7585, -1880.9485, -1405.4272, -930.289, -467.04822, -18.867363,
421.17499, 838.86719, 1239.9121, 1626.0669, 1990.6389, 2334.0852,
2655.344, 2962.0227, 3243.7817, 3506.2249, 3744.2622, 3959.8271,
4156.7061, 4324.9048, 4469.229, 4591.6689, 4687.4194, 4764.0801,
4814.6167, 4840.313, 4846.0181, 4826.3135, 4777.6553, 4696.0791,
4583.854, 4442.457, 4272.5254, 4076.7224, 3851.1211, 3603.1853,
3330.7456, 3038.3157, 2724.115, 2386.5476, 2032.5809, 1660.0547,
1268.0084, 859.16675, 432.4075, -14.131592, -479.29309, -955.67108,
-1444.614, -1937.2562, -2437.0085, -2941.8914, -3450.9009, -3959.9597,
-4468.9795, -4981.2549, -5492.6997, -6002.334, -6510.5425, -7016.2432,
-7517.8286, -8013.1348, -8500.4199, -8974.8867, -9439.5479, -9890.5938,
-10326.367, -10744.421, -11147.754, -11534.83, -11902.651, -12248.997,
-12577.919, -12885.458, -13172.309, -13441.554, -13691.502, -13922.634,
-14127.116, -14305.272, -14458.267, -14582.934, -14685.274, -14758.539,
-14806.058, -14830.719, -14836.625, -14822.204, -14773.916, -14700.484,
-14597.968, -14469.834, -14312.099, -14126.422, -13915.136, -13676.505,
-13412.388, -13120.703, -12807.961, -12473.883, -12115.751, -11740.082,
-11342.633, -10929.945, -10502.158, -10062.869, -9611.8271, -9146.6006,
-8673.3545, -8191.7417, -7700.769, -7200.9346, -6695.8809, -6185.2378,
-5670.8711, -5154.9995, -4643.4414, -4135.0015, -3629.2859, -3125.657,
-2626.541, -2134.0662, -1646.4242, -1168.816, -699.63068, -245.34488,
192.7984, 618.76703, 1033.223, 1428.922, 1807.2645, 2165.6274,
2507.6655, 2826.2754, 3120.4724, 3395.2593, 3647.6946, 3879.4983,
4086.3855, 4265.1323, 4421.6831, 4554.3594, 4657.8184, 4736.9561,
4792.6724, 4822.3784, 4830.3091, 4815.9038, 4773.9692, 4706.4736,
4614.8379, 4491.3198, 4337.8892, 4158.002, 3949.3147, 3713.4622,
3453.9114, 3167.8179, 2861.2598, 2536.3259, 2187.3623, 1822.752,
1437.5449, 1034.8208, 617.23962, 183.35637, -270.79733, -738.95618,
-1220.1345, -1710.7787, -2206.1941, -2706.4871, -3210.8625, -3721.0002,
-4233.6387, -4747.7271, -5258.7578, -5771.3071, -6280.7759, -6791.0166,
-7295.0229, -7794.4199, -8287.4189, -8771.6377, -9243.3457, -9702.2559,
-10146.865, -10577.053, -10989.863, -11385.981, -11760.477, -12116.938,
-12456.351, -12772.688, -13071.995), No.Rows = c(1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,
127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152,
153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165,
166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178,
179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230,
231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243,
244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256,
257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269,
270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282,
283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295,
296, 297, 298, 299, 300)), row.names = c(NA, 300L), class = "data.frame") class =
"data.frame")
I find the peaks and valleys of the data using:
library(quantmod)
max <- findPeaks(try$Actuator.Force)
min <- findValleys(try$Actuator.Force)
The result is the row.number of the try data.frame corresponding to the peaks and valleys. What I want is a vector of the Actuator.Force peak values corresponding to the row.numbers that the findPeaks and findValleys function find.
If the min and max values are the row index of try data frame, you can get a subset of try:
> try[min, ]
Actuator.Force No.Rows
5 -404.0766 5
97 -5043.0415 97
193 -14822.2040 193
> try[max, ]
Actuator.Force No.Rows
3 -402.7260 3
7 -407.5543 7
133 4826.3135 133
253 4815.9038 253
If you want to get only the Actuator.Force values for max and min row index:
> try[min, "Actuator.Force"]
[1] -404.0766 -5043.0415 -14822.2040
> try[max, "Actuator.Force"]
[1] -402.7260 -407.5543 4826.3135 4815.9038

Reverse leaf order in dendrogram using R

I have tried for several days to just flip a dendrogram so that the last gene is the first in the figure and the first the last. But even when I have managed to move leaves around the internal ordering is not the same. Here is my script:
cluster.hosts <- read.table("Norm_0_to1_heatmap.txt", header = TRUE, sep="", quote="/", row.names = 1)
# A table with 8 columnns and 229 rows cirresponding to gene expression
hosts.dist <- dist(cluster.hosts, method = "euclidean", diag = FALSE, upper = FALSE, p = 2)
hc <- hclust(hosts.dist, method = "average")
dd <- as.dendrogram(hc)
order.dendrogram(dd)
X11()
par(cex=0.5,font=3)
plot(dd, main="Dendrogram of Syn9 genes")
order.dd <- order.dendrogram(dd) #the numbers in the order indicate the position of the gene in the original table
#Then I generate a vector with the opposed order to the one obtained
y <- c(206, 204, 210, 209, 213, 212, 211, 207, 208, 94, 199, 192, 195, 198, 193, 201, 203, 200, 185, 61, 191, 190, 197, 189, 188, 196, 187, 215, 214, 202, 217, 220, 219, 218, 95, 180, 179, 181, 182, 186, 178, 132, 133, 122, 66, 65, 64, 58, 91, 88, 92, 89, 62, 184, 103, 128, 127, 229, 231, 230, 148, 63, 228, 116, 134, 104, 221, 78, 20, 232, 160, 159, 225, 112, 167, 164, 166, 140, 222, 51, 149, 227, 79, 68, 90, 131, 130, 136, 135, 105, 147, 172, 150, 176, 175, 174, 177, 152, 151, 165, 137, 168, 163, 52, 146, 141, 145, 82, 81, 56, 161, 120, 144, 129, 84, 1, 173, 143, 142, 86, 85, 83, 194, 183, 111, 55, 53, 54, 224, 171, 170, 223, 169, 93, 59, 60, 123, 121, 124, 87, 125, 226, 3, 158, 47, 10, 162, 138, 139, 154, 153, 119, 118, 117, 106, 80, 45, 70, 69, 126, 205, 77, 67, 19, 102, 46, 13, 108, 107, 109, 72, 71, 73, 23, 22, 25, 57, 48, 216, 155, 29, 24, 101, 35, 113, 115, 36, 37, 114, 110, 2, 14, 6, 16, 15, 17, 18, 74, 31, 30, 76, 12, 75, 8, 11, 5, 7, 99, 98, 100, 39, 38, 33, 32, 97, 96, 49, 44, 34, 50, 156, 26, 157, 42, 41, 43, 4, 28, 27, 9, 40, 21)
rx <- reorder(dd, y, agglo.FUN=mean)
order.rx <- order.dendrogram(rx)
write(order.rx, file="order_hosts_rx.txt", sep="\t")
write(labels(rx), file="labels_order_hosts_rx.txt", sep="\t")
X11()
par(cex=0.5)
plot(rx, main="Dendrogram of Syn9 genes")
I guess it has something to do with the heights of the leaves but I just want to flip the dendrogram...
Thanks in advance!
Miguel
You can use rev(dd); rev.dendrogram simply returns the dendrogram with reversed nodes:
hc <- hclust(dist(USArrests), "ave")
dd <- as.dendrogram(hc)
plot(dd)
plot(rev(dd))

Resources