This question already has answers here:
ggplot2 and date on x-axis
(1 answer)
Problems with changing the date scale on an axis - ggplot
(1 answer)
Closed 7 months ago.
I wish to plot my data by using the date on the x axis and the number of project launched on the y axis
I am currently using this code
plot1 <- ggplot()+
geom_line(data=data0,mapping = aes(x = date, y = launches, group=1) ) +
geom_line(data=data0,mapping = aes(x = date, y = US, group=1), colour="blue" )+
ggtitle("Kickstarter")
However, i realised that i have so many dates that the Y axis becomes difficult to read
Instead i would wish for the Y axis to just show the months
Is there any way to do this ?
structure(list(date = c("2021-01-01", "2021-01-02", "2021-01-03",
"2021-01-04", "2021-01-05", "2021-01-06"), launches = c(4, 0,
0, 0, 8, 4), pledged = c(50278.64, 0, 0, 0, 366279.590415302,
172073.0471292), backers = c(2880, 0, 0, 0, 6588, 3528), total_goal = c(24000,
0, 0, 0, 148000, 60000), mean_goal = c(6000, 0, 0, 0, 18500,
15000), US = c(4, 0, 0, 0, 4, 0), `number of success` = c(4,
0, 0, 0, 8, 4), duration_days = c(30, 0, 0, 0, 31, 30), Twitter = c(1324L,
1548L, 1297L, 1585L, 1636L, 1583L), replies = c(882L, 1252L,
910L, 1018L, 810L, 1000L), likes = c(22859L, 24375L, 17854L,
20341L, 19521L, 19401L), retweets = c(8621L, 8239L, 6141L, 6728L,
6938L, 6842L)), row.names = c(NA, 6L), class = "data.frame")
edit: my apologies, i inversed teh x and y axis in my explanation
Assume you meant date on x axis
library(tidyverse)
library(lubridate)
data0$date<-ymd(data0$date)
data0$month<-months(data0$date)
plot1 <- ggplot()+
geom_line(data=data0,mapping = aes(x = date, y = launches, group=1) ) +
geom_line(data=data0,mapping = aes(x = date, y = US, group=1), colour="blue" )+
ggtitle("Kickstarter")
I changed your 'example' dataset to better illustrate a potential solution:
library(ggplot2)
data0 <- structure(list(date = c("2021-01-01", "2021-01-15", "2021-02-01",
"2021-02-15", "2021-03-01", "2021-03-15"), launches = c(4, 0,
0, 0, 8, 4), pledged = c(50278.64, 0, 0, 0, 366279.590415302,
172073.0471292), backers = c(2880, 0, 0, 0, 6588, 3528), total_goal = c(24000,
0, 0, 0, 148000, 60000), mean_goal = c(6000, 0, 0, 0, 18500,
15000), US = c(4, 0, 0, 0, 4, 0), `number of success` = c(4,
0, 0, 0, 8, 4), duration_days = c(30, 0, 0, 0, 31, 30), Twitter = c(1324L,
1548L, 1297L, 1585L, 1636L, 1583L), replies = c(882L, 1252L,
910L, 1018L, 810L, 1000L), likes = c(22859L, 24375L, 17854L,
20341L, 19521L, 19401L), retweets = c(8621L, 8239L, 6141L, 6728L,
6938L, 6842L)), row.names = c(NA, 6L), class = "data.frame")
plot1 <- ggplot(data0) +
geom_line(aes(x = date, y = launches, group = 1) ) +
geom_line(aes(x = date, y = US, group = 1), colour="blue") +
ggtitle("Kickstarter")
plot1
# Change the format from "character" to "date"
data0$date <- as.Date(data0$date)
# Then you can change the breaks on the x axis
plot2 <- ggplot(data0) +
geom_line(aes(x = date, y = launches, group = 1) ) +
geom_line(aes(x = date, y = US, group = 1), colour="blue") +
ggtitle("Kickstarter") +
scale_x_date(date_breaks = "1 month")
plot2
Created on 2022-07-27 by the reprex package (v2.0.1)
Does this solve your problem?
Related
I would like to plot an environmental variable on a ggplot2 version of a DCA plot.
I have some code where I extract species and data scores from vegan and then plot them up in ggplot2. I am having trouble trying to work out how I can get my environmental variable SWLI to plot as an arrow - something like this RDA's plots with ggvegan: How can I change text position for arrows text? (or see PCA example here https://www.rpubs.com/an-bui/vegan-cheat-sheet)
Can anybody help?
#DCA Plot
library(plyr)
library(vegan)
library(ggplot2)
library(cluster)
library(ggfortify)
library(factoextra)
#read in csv and remove variables you don't want to go through analysis
regforamcountsall<-read_csv("regionalforamcountsallnocalcs.csv")
swli<-read_csv("DCAenv.csv")
rownames(regforamcountsall)<-regforamcountsall$Sample
regforamcountsall$Sample = NULL
regforamcountsall$Site=NULL
regforamcountsall$SWLI=NULL
#check csv
regforamcountsall
#run ordination
ord<-decorana(regforamcountsall)
#get species scores
summary(ord)
#get DCA values of environmental variable
ord.fit <- envfit(ord ~ SWLI, data=swli, perm=999)
ord.fit
plot(ord, dis="site")
plot(ord.fit)
#use this summary code to get species scores for DCA1 and DCA2
#put species scores values in from ord plot summary stats
species.scores<-read.csv("speciescores.csv")
species.scores$species <- row.names(species.scores)
#Using the scores function from vegan to extract the sample scores and convert to a data.frame
data.scores <- as.data.frame(scores(ord))
# create a column of groupings/clusters, from the rownames of data.scores
data.scores$endgroup <- as.factor(pam(regforamcountsall, 3)$clustering)
#getting the convex hull of each unique point set
find_hull <- function(df) df[chull(data.scores$DCA1, data.scores$DCA2), ]
hulls <- NULL
for(i in 1:length(unique(data.scores$endgroup))){
endgroup_coords <- data.scores[data.scores$endgroup == i,]
hull_coords <- data.frame(
endgroup_coords[chull(endgroup_coords[endgroup_coords$endgroup == i,]$DCA1,
endgroup_coords[endgroup_coords$endgroup == i,]$DCA2),])
hulls <- rbind(hulls,hull_coords)
}
data.scores$numbers <- 1:length(data.scores$endgroup)
regforamcountsall<-read_csv("regionalforamcountsallnocalcs.csv")
rownames(regforamcountsall)<-regforamcountsall$Sample
data.scores$Site<-regforamcountsall$Site
data.scores$SWLI<-regforamcountsall$SWLI
data.scores
#DCA with species
data.scores$Site <- as.character(data.scores$Site)
library(scico)
dca <- ggplot() +
# add the point markers
geom_point(data=data.scores,aes(x=DCA1,y=DCA2,colour=SWLI,pch=Site),size=4) + geom_point(data=species.scores,aes(x=DCA1,y=DCA2),size=3,pch=3,alpha=0.8,colour="grey22") +
# add the hulls and labels - numbers position labels
geom_polygon(data = hulls,aes(x=DCA1,y=DCA2,fill=endgroup), alpha = 0.25) +
#geom_text(data=data.scores,aes(x=DCA1-0.03,y=DCA2,colour=endgroup, label = numbers))+
geom_text(data=species.scores,aes(x=DCA1+0.1,y=DCA2+0.1, label = species))+
#look this up
geom_segment(data=ord.fit,aes(x = 0, y = 0, xend=DCA1,yend=DCA2), arrow = arrow(length = unit(0.3, "cm")))+
theme_classic()+
scale_color_scico(palette = "lapaz")+
coord_fixed()
dca
#regforamcountsall data
structure(list(Sample = c("T3LB7.008", "T3LB7.18", "T3LB7.303",
"WAP 0 ST-2", "T3LB7.5", "LG120"), T.salsa = c(86.63793102, 68.5897436,
70.39274924, 5.199999999, 79.15057916, 44.40000001), H.wilberti = c(0,
0, 0, 0, 0.386100386, 9.399999998), Textularia = c(0, 0, 0, 0,
0, 0.4), T.irregularis = c(2.155172414, 10.25641026, 7.854984897,
0, 2.702702703, 0), P.ipohalina = c(0, 0, 0, 0, 0, 0), J.macrescens = c(4.741379311,
5.769230769, 4.833836859, 5.800000001, 8.108108107, 5.400000001
), T.inflata = c(6.465517244, 15.38461538, 16.918429, 83.2, 5.791505794,
40.4), S.lobata = c(0, 0, 0, 2.300000001, 0, 0), M.fusca = c(0,
0, 0, 3.499999999, 3.861003862, 0), A.agglutinans = c(0, 0, 0,
0, 0, 0), A.exiguus = c(0, 0, 0, 0, 0, 0), A.subcatenulatus = c(0,
0, 0, 0, 0, 0), P.hyperhalina = c(0, 0, 0, 0, 0, 0), SWLI = c(200,
197.799175, 194.497937, 192.034776, 191.746905, 190.397351),
Site = c("LSP", "LSP", "LSP", "WAP", "LSP", "LG")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
#data.scores
structure(list(DCA1 = c(-1.88587476921648, -1.58550534382589,
-1.59816311314591, -0.0851161831632892, -1.69080448670088, -1.14488987340879
), DCA2 = c(0.320139736602921, 0.226662031865046, 0.230912045301637,
-0.0531232712001122, 0.272143119753744, 0.0696939776869396),
DCA3 = c(-0.755595015095353, -0.721144380683279, -0.675071834919103,
0.402339366526422, -0.731006052784081, 0.00474996849420783
), DCA4 = c(-1.10780013276303, -0.924265835490466, -0.957711953532202,
-0.434438970032073, -0.957873836258657, -0.508347000558056
), endgroup = structure(c(1L, 1L, 1L, 2L, 1L, 1L), .Label = c("1",
"2", "3"), class = "factor"), numbers = 1:6, Site = c("LSP",
"LSP", "LSP", "WAP", "LSP", "LG"), SWLI = c(200, 197.799175,
194.497937, 192.034776, 191.746905, 190.397351)), row.names = c(NA,
6L), class = "data.frame")
#species.scores
structure(list(species = c("1", "2", "3", "4", "5", "6"), DCA1 = c(-2.13,
-1.6996, -2.0172, -0.9689, 1.0372, -0.3224), DCA2 = c(0.342,
-0.8114, 0.3467, -0.3454, 2.0007, 0.9147)), row.names = c(NA,
6L), class = "data.frame")
Here's some data
structure(list(Period = structure(c(2017.83333333333, 2017.91666666667,
2018, 2018.08333333333, 2018.16666666667, 2018.25, 2018.33333333333,
2018.41666666667, 2018.5, 2018.58333333333, 2018.66666666667,
2018.75, 2018.83333333333, 2018.91666666667, 2019, 2019.08333333333,
2019.16666666667, 2019.25, 2019.33333333333, 2019.41666666667,
2019.5), class = "yearmon"), neg = c(0, 0, 0, 0, 0, 0, 0, 0,
-0.782066446199374, -1.33087717414387, -1.55401649141939, -1.9056578851487,
-2.19869230289699, -1.99579537718088, -2.03857957860623, -2.14184701726747,
-2.27461866979037, -2.39022691659445, -2.3732334198156, -1.83686080707261,
-1.86553025598681), pos = c(0.550567625206492, 0.699954781241267,
0.775518140437689, 0.647367030217637, 0.84562688020279, 0.923814518387379,
0.686796306801202, 0.131849327496122, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)), row.names = 960:980, class = "data.frame")
I want to plot the SPEI values with ggplot as I learned it here:
How to format the x-axis of the hard coded plotting function of SPEI package in R?
library(ggplot2)
ggplot(test) +
geom_area(aes(x = Period, y = pos), fill = "blue", col = "black") +
geom_area(aes(x = Period, y = neg), fill = "red", col = "black") +
scale_y_continuous(limits = c(-2.25, 2.25),
breaks = -2:2) +
ylab("SPEI") + xlab("") +
theme_bw()
The result looks like this:
As you can see, when the sign changes from positive to negative, geom_area doesn't end/start at the same position. Anyone any idea how to fix this? I thought about using Date instead of yearmon, but got stuck with the same problem.
This is a gates and posts problem: Each geom_area at the inflection is starting and ending on a post, hence the overlap. They should be starting in the middle of the gate between the posts.
This solution may be a bit heavy handed but I think it should apply where there are multiple changes from positive to negative and vice versa.
library(ggplot2)
library(tidyr)
library(tibble)
library(dplyr)
library(lubridate)
library(imputeTS)
Determine when the data changes from positive to negative or vice versa
inflections <-
test %>%
mutate(inflect = case_when(lag(neg) == 0 & pos == 0 ~ TRUE,
lag(pos) == 0 & neg == 0 ~ TRUE,
TRUE ~ FALSE),
rowid = row_number() - 0.5) %>%
filter(inflect) %>%
select(-inflect) %>%
mutate(Period = NA_Date_,
pos = 0,
neg = 0)
Insert a new row to mark the inflection point to allow inclusion of an intermediary time where both pos and neg can be zero.
test1 <-
test %>%
rowid_to_column() %>%
bind_rows(inflections) %>%
arrange(rowid)
Impute a time when the data changes from pos to neg with a function from imputeTS.
test1$Period <- na_interpolation(as.ts(test1$Period))
plot
ggplot(test1) +
geom_area(aes(x = Period, y = pos), fill = "blue", col = "black") +
geom_area(aes(x = Period, y = neg), fill = "red", col = "black") +
scale_y_continuous(limits = c(-2.25, 2.25),
breaks = -2:2) +
ylab("SPEI") + xlab("") +
theme_bw()
data
```
test <- structure(list(Period = structure(c(2017.83333333333, 2017.91666666667,
2018, 2018.08333333333, 2018.16666666667, 2018.25, 2018.33333333333,
2018.41666666667, 2018.5, 2018.58333333333, 2018.66666666667,
2018.75, 2018.83333333333, 2018.91666666667, 2019, 2019.08333333333,
2019.16666666667, 2019.25, 2019.33333333333, 2019.41666666667,
2019.5), class = "yearmon"), neg = c(0, 0, 0, 0, 0, 0, 0, 0,
-0.782066446199374, -1.33087717414387, -1.55401649141939, -1.9056578851487,
-2.19869230289699, -1.99579537718088, -2.03857957860623, -2.14184701726747,
-2.27461866979037, -2.39022691659445, -2.3732334198156, -1.83686080707261,
-1.86553025598681), pos = c(0.550567625206492, 0.699954781241267,
0.775518140437689, 0.647367030217637, 0.84562688020279, 0.923814518387379,
0.686796306801202, 0.131849327496122, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)), row.names = 960:980, class = "data.frame")
```
<sup>Created on 2020-05-22 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>
I have two character vectors of equal length; where position one in vector.x matches position one in vector.y and so on. The elements refer to column names in a data frame (wide format). I would like to somehow loop through these vectors to produce xy scatter graphs for each pair in the vector, preferably in a faceted plot. Here is a (hopefully) reproducible example. To be clear, with this example, I would end up with 10 scatter graphs.
vector.x <- c("Aplanochytrium", "Aplanochytrium", "Aplanochytrium", "Aplanochytrium", "Aplanochytrium", "Bathycoccus", "Brockmanniella", "Brockmanniella", "Caecitellus_paraparvulus", "Caecitellus_paraparvulus")
vector.y <- c("Aliiroseovarius", "Neptuniibacter", "Pseudofulvibacter", "Thalassobius", "unclassified_Porticoccus", "Tenacibaculum", "Pseudomonas", "unclassified_GpIIa", "Marinobacter", "Thalassobius")
structure(list(Aliiroseovarius = c(0, 0, 0, 0.00487132352941176,
0.0108639420589757), Marinobacter = c(0, 0.00219023779724656,
0, 0.00137867647058824, 0.00310398344542162), Neptuniibacter = c(0.00945829750644884,
0.00959532749269921, 0.0171310629514964, 0.2796875, 0.345835488877393
), Pseudofulvibacter = c(0, 0, 0, 0.00284926470588235, 0.00362131401965856
), Pseudomonas = c(0.00466773123694878, 0.00782227784730914,
0.0282765737874097, 0.00707720588235294, 0.00400931195033627),
Tenacibaculum = c(0, 0, 0, 0.00505514705882353, 0.00362131401965856
), Thalassobius = c(0, 0.00166875260742595, 0, 0.0633272058823529,
0.147697878944646), unclassified_GpIIa = c(0, 0.000730079265748853,
0, 0.003125, 0.00103466114847387), unclassified_Porticoccus = c(0,
0, 0, 0.00119485294117647, 0.00569063631660631), Aplanochytrium = c(0,
0, 0, 0.000700770847932726, 0.0315839846865529), Bathycoccus = c(0.000388802488335925,
0, 0, 0.0227750525578136, 0.00526399744775881), Brockmanniella = c(0,
0.00383141762452107, 0, 0.000875963559915907, 0), Caecitellus_paraparvulus = c(0,
0, 0, 0.000875963559915907, 0.00797575370872547)), row.names = c("B11",
"B13", "B22", "DI5", "FF6"), class = "data.frame")
As Rui Barradas shows, it's possible to get a very nice plot from ggplot and gridExta. If you wanted to stick to base R, here's how you'd do that (assuming your data set is called df1):
# set plot sizes
par(mfcol = c(floor(sqrt(length(vector.x))), ceiling(sqrt(length(vector.x)))))
# loop through plots
for (i in 1:length(vector.x)) {
plot(df1[[vector.x[i]]], df1[[vector.y[i]]], xlab = vector.x[i], ylab = vector.y[i])
}
# reset plot size
par(mfcol = c(1,1))
This is a bit long and convoluted but it works.
library(tidyverse)
library(gridExtra)
df_list <- apply(data.frame(vector.x, vector.y), 1, function(x){
DF <- df1[which(names(df1) %in% x)]
i <- which(names(DF) %in% vector.x)
if(i == 2) DF[2:1] else DF
})
gg_list <- lapply(df_list, function(DF){
ggplot(DF, aes(x = get(names(DF)[1]), y = get(names(DF)[2]))) +
geom_point() +
xlab(label = names(DF)[1]) +
ylab(label = names(DF)[2])
})
g <- do.call(grid.arrange, gg_list)
g
Not too elegant, but should get you going:
vector.x <- c("Aplanochytrium", "Aplanochytrium", "Aplanochytrium", "Aplanochytrium", "Aplanochytrium", "Bathycoccus", "Brockmanniella", "Brockmanniella", "Caecitellus_paraparvulus", "Caecitellus_paraparvulus")
vector.y <- c("Aliiroseovarius", "Neptuniibacter", "Pseudofulvibacter", "Thalassobius", "unclassified_Porticoccus", "Tenacibaculum", "Pseudomonas", "unclassified_GpIIa", "Marinobacter", "Thalassobius")
df1 = structure(
list(Aliiroseovarius = c(0, 0, 0, 0.00487132352941176, 0.0108639420589757),
Marinobacter = c(0, 0.00219023779724656, 0, 0.00137867647058824, 0.00310398344542162),
Neptuniibacter = c(0.00945829750644884, 0.00959532749269921, 0.0171310629514964, 0.2796875, 0.345835488877393),
Pseudofulvibacter = c(0, 0, 0, 0.00284926470588235, 0.00362131401965856),
Pseudomonas = c(0.00466773123694878, 0.00782227784730914, 0.0282765737874097, 0.00707720588235294, 0.00400931195033627),
Tenacibaculum = c(0, 0, 0, 0.00505514705882353, 0.00362131401965856),
Thalassobius = c(0, 0.00166875260742595, 0, 0.0633272058823529, 0.147697878944646),
unclassified_GpIIa = c(0, 0.000730079265748853, 0, 0.003125, 0.00103466114847387),
unclassified_Porticoccus = c(0, 0, 0, 0.00119485294117647, 0.00569063631660631),
Aplanochytrium = c(0, 0, 0, 0.000700770847932726, 0.0315839846865529),
Bathycoccus = c(0.000388802488335925, 0, 0, 0.0227750525578136, 0.00526399744775881),
Brockmanniella = c(0, 0.00383141762452107, 0, 0.000875963559915907, 0),
Caecitellus_paraparvulus = c(0, 0, 0, 0.000875963559915907, 0.00797575370872547)),
row.names = c("B11", "B13", "B22", "DI5", "FF6"),
class = "data.frame"
)
df2 = NULL
for(i in 1:10) {
df.tmp = data.frame(
plot = paste0(vector.x[i], ":", vector.y[i]),
x = df1[[vector.x[i]]],
y = df1[[vector.y[i]]]
)
if(is.null(df2)) df2=df.tmp else df2 = rbind(df2, df.tmp)
}
ggplot(data=df2, aes(x, y)) +
geom_point() +
facet_grid(cols = vars(plot))
My data (final.df) looks like the following:
A B C Y 1
0 0 0 0 0.05
0 0 1 1 0.03
....
Based on the comment below, here is a ASCII text representation of the dataframe.
structure(list(A = c(502, 541, 542, 543, 544, 545, 4304, 4370,
4371, 4372, 4373, 4442), B = c(4.4, 4.2, 4.4, 4.6, 4.8, 5, 5.2,
4.6, 4.8, 5, 5.2, 5.2), C = c(2.6, 2.8, 2.8, 2.8, 2.8, 2.8, 12.6,
12.8, 12.8, 12.8, 12.8, 13), Y = c(1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1), `1` = c(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1), `NA` = c(0,
0, 0, 0, 0, 0, 0, 0, 0.000281600479875937, 0, 0, 0)), .Names = c("A",
"B", "C", "Y", "1", NA), row.names = c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L), class = "data.frame")
To summarize, there are four columns that identify each data point. I am interested in creating two boxplots according to their values in column with name 1. I want to compare the values for points labeled 0 in column 'Y' and labeled 1 in column 'Y'. Finally, I want to be able to hover over the points to retrieve the meta-data, meaning the 'A', 'B', 'C', and '1' value.
p <- ggplot(final.df, aes(x = factor(Y), y =
Y, fill = factor(Y)))
p <- p + geom_boxplot() + geom_point() + xlab("Y") + guides(fill =
guide_legend("Y")) + theme(legend.position="top")
final.p <- ggplotly(p)
The current plot shows me factor(Y) value and the corresponding value in 1. How can I include the meta-data in columns 'A', 'B', 'C'?
We can build a text using paste0 and HTML tag <br><\br> and instructe toolttip to use text.
p <- ggplot(df, aes(x = factor(Y), y = Y,
fill = factor(Y), text=paste('</br>A: ',A,'</br>B: ',B, '</br>1: ',1)))
ggplotly(p,tooltip = c("text"))
Use the tooltip feature of ggplotly. Read about it by typing in help(ggplotly). See Below:
library(tidyverse)
library(plotly)
set.seed(55)
df <- data.frame(
A = c(rep(0, 8), rep(1, 8)),
B = rep(c(rep(0, 4), rep(1, 4)), 2),
C = rep(c(rep(0, 2), rep(1, 2)), 4),
Y = rep(c(0, 1), 8),
X1 = runif(16)
)
p <- ggplot(df, aes(x = factor(Y), y = X1, fill = factor(Y), A = A, B = B, C = C))
p <- p + geom_boxplot() +
geom_point() +
xlab("Y") +
guides(fill = guide_legend("Y")) +
theme(legend.position = "top")
final.p <- ggplotly(p, tooltip = c("A", "B", "C"))
final.p
I have a question about line colours in ggplot2. I need to plot solar radiation data but I only have 6 hourly data, so geom_line doest not give a "nice" outuput. I've tried geom_smooth and the result is close to what I need. But I have a new question, is it possible to change line colour depending on the y value?
The code used for the plot is
library(ggplot2)
library(lubridate)
# Lectura de datos
datos.uvi=read.csv("serie-temporal-1.dat",sep=",",header=T,na.strings="-99.9")
datos.uvi=within(datos.uvi, fecha <- ymd_h(datos.uvi$fecha.hora))
# geom_smooth
ggplot(data=datos.uvi, aes(x=fecha, y=Rad_Global_.mW.m2., colour="GLOBAL")) +
geom_smooth(se=FALSE, span=0.3)
In the desired output, line should be red for radiation values under 250, green in the 250-500 interval and blue for values higher than 500.
Is it possible with geom_smooth? I've tried to reuse code here, but could not find the point.
Data used for the plot:
dput(datos.uvi)
structure(list(fecha.hora = c(2016012706L, 2016012712L, 2016012718L,
2016012800L, 2016012806L, 2016012812L, 2016012818L, 2016012900L,
2016012906L, 2016012912L, 2016012908L, 2016013000L), latitud = c(37.75,
37.75, 37.75, 37.75, 37.75, 37.75, 37.75, 37.75, 37.75, 37.75,
37.75, 37.75), longitud = c(-1.25, -1.25, -1.25, -1.25, -1.25,
-1.25, -1.25, -1.25, -1.25, -1.25, -1.25, -1.25), altitud = c(300L,
300L, 300L, 300L, 300L, 300L, 300L, 300L, 300L, 300L, 300L, 300L
), cobertura_nubosa = c(0.91, 0.02, 0.62, 1, 0.53, 0.49, 0.01,
0, 0, 0.13, 0.62, 0.84), longitud_de_onda_inicial.nm. = c(284.55,
284.55, 284.55, 284.55, 284.55, 284.55, 284.55, 284.55, 284.55,
284.55, 284.55, 284.55), Rad_Global_.mW.m2. = c(5e-04, 259.2588,
5, 100.5, 1, 886.5742, 110, 40, 20, 331.3857, 0, 0), Rad_Directa_.mW.m2. = c(0,
16.58034, 0, 0, 0, 202.5683, 0, 0, 0, 89.81712, 0, 0), Rad_Difusa_.mW.m2. = c(0,
242.6785, 0, 0, 0, 684.0059, 0, 0, 0, 241.5686, 0, 0), Angulo_zenital_.º. = c(180,
56.681, 180, 180, 180, 56.431, 180, 180, 180, 56.176, 180, 180
), blank = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
fecha = structure(c(1453874400, 1453896000, 1453917600, 1453939200,
1453960800, 1453982400, 1454004000, 1454025600, 1454047200,
1454068800, 1454054400, 1454112000), tzone = "UTC", class = c("POSIXct",
"POSIXt"))), row.names = c(NA, -12L), .Names = c("fecha.hora",
"latitud", "longitud", "altitud", "cobertura_nubosa", "longitud_de_onda_inicial.nm.",
"Rad_Global_.mW.m2.", "Rad_Directa_.mW.m2.", "Rad_Difusa_.mW.m2.",
"Angulo_zenital_.º.", "blank", "fecha"), class = "data.frame")
Thanks in advance.
Calculate the smoothing outside ggplot2 and then use geom_segment:
fit <- loess(Rad_Global_.mW.m2. ~ as.numeric(fecha), data = datos.uvi, span = 0.3)
#note the warnings
new.x <- seq(from = min(datos.uvi$fecha),
to = max(datos.uvi$fecha),
by = "5 min")
new.y <- predict(fit, newdata = data.frame(fecha = as.numeric(new.x)))
DF <- data.frame(x1 = head(new.x, -1), x2 = tail(new.x, -1) ,
y1 = head(new.y, -1), y2 = tail(new.y, -1))
DF$col <- cut(DF$y1, c(-Inf, 250, 500, Inf))
ggplot(data=DF, aes(x=x1, y=y1, xend = x2, yend = y2, colour=col)) +
geom_segment(size = 2)
Note what happens at the cut points. If might be more visually appealing to make the x-grid for prediction very fine and then use geom_point instead. However, plotting will be slow then.
This is not really what you asked for, but might serve the same purpose: instead of colouring the line, colour the background. First we create a dataframe of rectangle/limit coordinates.
rect_data <- data.frame(xmin=min(datos.uvi$fecha),
xmax=max(datos.uvi$fecha),
ymin=c(0,250,500),
ymax=c(250,500,max(datos.uvi$Rad_Global_.mW.m2.)),
col=c("red","green","blue"))
Then we add them to the plot, using scale_fill_identity()
ggplot(data=datos.uvi) +
geom_smooth(aes(x=fecha, y=Rad_Global_.mW.m2.),colour="black",se=FALSE, span=0.3) +
geom_rect(data=rect_data, aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax,fill=col),alpha=0.1)+
scale_fill_identity()