R; package {stats}; function {ts.plot}; change x-axis labeles
I need to change the x-axis labels so they are words instead of numerals. Currently, the x-axis goes 1, 2, 3, etc. I would like it to go "Placement", "Fresh", etc.
trendplot <- function(timeseries, color, index, first = FALSE, w = 4, add_main = ""){
if (first) {
plot.ts(timeseries[, index], col = color, lwd = w,
ylim = c(-2, 2),
ylab = "Scaled Relative Abundance",
xlab = "Stage",
main = paste0("Trend Plots", add_main)) # gsub(".", " ", colnames(timeseries)[index], fixed = TRUE)
} else {
lines(timeseries[, index], col = color, lwd = w)
}
}
Update: This code works for what I wanted it be.
stages<-c('Placement','Fresh','Bloat One','Bloat Two','Post-Bloat One','Post-Bloat Two')
trendplot <- function(timeseries, color, index, first = FALSE, w = 4, add_main = ""){
if (first) {
plot.ts(timeseries[, index], col = color, lwd = w, axes = FALSE,
ylim = c(-2, 2),
ylab = "Scaled Measurement",
xlab = "Time Point",
main = paste0("Trend Plots", add_main)) # gsub(".", " ", colnames(timeseries)[index], fixed = TRUE)+
} else {
lines(timeseries[, index], col = color, lwd = w)
}
{axis(1, at=1:6, labels=stages, cex.axis=0.5)}
}
do you know?
how many words do you know
See also questions close to this topic
-
pivot_wider does not keep all the variables
I would like to keep the variable
cat
(category) in the output of my function. However, I am not able to keep it. The idea is to apply a similar function tom <- 1 - (1 - se * p2)^df$n
based on the category. But in order to perform that step, I need to keep the variable category.Here's the code:
#script3 suppressPackageStartupMessages({ library(mc2d) library(tidyverse) }) sim_one <- function() { df<-data.frame(id=c(1:30),cat=c(rep("a",12),rep("b",18)),month=c(1:6,1,6,4,1,5,2,3,2,5,4,6,3:6,4:6,1:5,5),n=rpois(30,5)) nr <- nrow(df) df$n[df$n == "0"] <- 3 se <- rbeta(nr, 96, 6) epi.a <- rpert(nr, min = 1.5, mode = 2, max = 3) p <- 0.2 p2 <- epi.a*p m <- 1 - (1 - se * p2)^df$n results <- data.frame(month = df$month, m, df$cat) results %>% arrange(month) %>% group_by(month) %>% mutate(n = row_number(), .groups = "drop") %>% pivot_wider( id_cols = n, names_from = month, names_glue = "m_{.name}", values_from =m ) } set.seed(99) iters <- 1000 sim_list <- replicate(iters, sim_one(), simplify = FALSE) sim_list[[1]] #> # A tibble: 7 x 7 #> n m_1 m_2 m_3 m_4 m_5 m_6 #> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 1 0.970 0.623 0.905 0.998 0.929 0.980 #> 2 2 0.912 0.892 0.736 0.830 0.890 0.862 #> 3 3 0.795 0.932 0.553 0.958 0.931 0.798 #> 4 4 0.950 0.892 0.732 0.649 0.777 0.743 #> 5 5 NA NA NA 0.657 0.980 0.945 #> 6 6 NA NA NA 0.976 0.836 NA #> 7 7 NA NA NA NA 0.740 NA
Created on 2022-05-07 by the reprex package (v2.0.1)
-
calculate weighted average over several columns with NA
I have a data frame like this one:
ID duration1 duration2 total_duration quantity1 quantity2 1 5 2 7 3 1 2 NA 4 4 3 4 3 5 NA 5 2 NA
I would like to do a weighted mean for each subject like this:
df$weighted_mean<- ((df$duration1*df$quantity1) + (df$duration2*df$quantity2) ) / (df$total_duration)
But as I have NA, this command does not work and it is not very nice....
The result would be this:
ID duration1 duration2 total_duration quantity1 quantity2 weighted_mean 1 5 2 7 3 1 2.43 2 NA 4 4 3 4 4 3 5 NA 5 2 NA 2
Thanks in advance for the help
-
I am to extract data from netCDF file using R for specific loaction the code i've written as showen and I have an error at the end of the code
I need some help with extracting date from NetCDF files using R , I downloaded them from cordex (The Coordinated Regional climate Downscaling Experiment). In total I have some files. This files have dimensions of (longitude, latitude, time) and the variable is maximum temperature (tasmax). At specific location, I need to extract data of tasmax at different time. In total I have some files. This files have dimensions of (longitude, latitude, time) and variable maximum temperature (tasmax). At specific location, I need to extract data of tasmax at different time.I wrote the code using R but at the end of code, an error appeared. Error ( location subscript out of bounds)
getwd() setwd("C:/Users/20120/climate change/rcp4.5/tasmax")
dir() library ("ncdf4") libra,-ry(ncdf4.helpers) library ("chron") ncin <- nc_open("tasmax_AFR-44_ICHEC-EC-EARTH_rcp45_r1i1p1_KNMI-RACMO22T_v1_mon_200601-201012.nc") lat <- ncvar_get(ncin, "lat") lon <- ncvar_get(ncin, "lon") tori <- ncvar_get(ncin, "time") title <- ncatt_get(ncin,0,"title") institution <- ncatt_get(ncin,0,"institution") datasource <- ncatt_get(ncin,0,"source") references <- ncatt_get(ncin,0,"references") history <- ncatt_get(ncin,0,"history") Conventions <- ncatt_get(ncin,0,"Conventions") tustr <- strsplit(tunits$value,"") ncin$dim$time$units ncin$dim$time$calendar tas_time <- nc.get.time.series(ncin, v = "tasmax", time.dim.name = "time") tas_time[c(1:3, length(tas_time) - 2:0)] tmp.array <- ncvar_get(ncin,"tasmax") dunits <- ncatt_get(ncin,"tasmax","units") tmp.array <- tmp.array-273.15 tunits <- ncatt_get(ncin,"time","units") nc_close(ncin) which.min(abs(lat-28.9)) which.min(abs(lon-30.2)) tmp.slice <- tmp.array[126,32981,] tmp.slice
Error in tmp.array[126, 32981, ] : subscript out of bounds
-
R: Labels not displaying at a ggplot2 graph
Given this R script:
library(glue) library(ggplot2) ir.data <- read.csv(file="~/apps/mine/cajueiro_weather_station/sensor_data/temperature_data.csv", header = F) ir.data$V1 <- as.POSIXct(ir.data$V1, format = "%Y-%m-%dT%H:%M:%S", tz = "UTC") ir.data$size <- (ir.data$V2 - ir.data$V3) ggplot(ir.data, aes(x=V1)) + labs(title = "IR-radiation-based sky temperature monitoring.", subtitle = glue("Samples from {ir.data$V1[1]}h to {tail(ir.data$V1, n=1)}h UTC-3."), caption = "Cajueiro Weather Station - fschuindt.githhub.io/blog/weather") + geom_line(aes(y = V2), color = "#6163c2") + geom_line(aes(y = V3), color = "#ad1fa2") + scale_color_discrete(name = "Labels", labels = c("Ambient temperature.", "Sky temperature.")) + xlab("Timestamp") + ylab("Measured temperature in °Celcius")
And this .csv data sample:
2022-04-30T19:47:00,28.03,28.05 2022-04-30T19:47:02,27.99,28.01 2022-04-30T19:47:04,28.07,28.01 2022-04-30T19:47:06,28.05,28.05 2022-04-30T19:47:08,28.05,28.01 2022-04-30T19:47:10,28.03,28.01 2022-04-30T19:47:12,28.05,27.99 2022-04-30T19:47:14,28.07,28.01 2022-04-30T19:47:16,28.07,28.05 2022-04-30T19:47:18,28.05,28.05 2022-04-30T19:47:20,28.09,28.07
That's the plot output (the .csv data is bigger than the example):
Why the labels described at
scale_color_discrete(name = "Labels", labels = c("Ambient temperature.", "Sky temperature."))
are not being displayed? -
Add text annotations at consistent locations in facet_grid when scale = 'free_y' + ggplot2 + r
I need to annotate a set of chats in a facet grid where the y axis is scale is set to
scale = 'free_y'
.As the scales are very different, when I set the y position of the geom_text the text location is also very different for each graph. Is there any method to correct for this so that they are all in the same elate x,y position on each chart in the facet grid?
The example below demonstrates the problem:
library(ggplot2) df <- data.frame(name = c('Jim',"Bob", "Sue",'Jim',"Bob", "Sue",'Jim',"Bob", "Sue"), r = c(1,10,100,2,20,200,3,30,300), z = c(1,10,100,2,20,200,3,30,300)) p <- ggplot(df, aes(z, r)) + geom_line() p <- p + facet_grid(vars(name),scales = "free") dfl <- data.frame(name = c('Jim',"Bob", "Sue"), r = c(-0.2, 0.5, -0.4)) p + geom_text(data = dfl, aes(200, 10,label = r), check_overlap = T)
Ideally, in this example, the labels would all be the same position as the first chart in the facet gris "Bob".
I have reviewed this previous question, which addresses text annotation on a single chart in a facet grid, but not the placement in the case of different y scales per facet - Annotating text on individual facet in ggplot2
-
Add additional labels from a DataFrame to a facet_grid with existing label
I have a set data that I need to add to levels of labels. One on a single chart within the facet grid, and one from a small dataframe with entries for for each chart.
In the example below you'll see that I can add to a single chart no problem but when I try to add from the df I get the error -
Error in FUN(X[[i]], ...) : object 'wt' not found
Preparation:
library(ggplot2) p <- ggplot(mtcars, aes(mpg, wt)) + geom_line() p <- p + facet_grid(. ~ cyl) ann_text <- data.frame(mpg = 30,wt = 5,lab = "Text", cyl = factor(8,levels = c("4","6","8"))) dfl <- data.frame(name = c('Jim',"Bob", "Sue"), r = c(-0.2, 0.5, -0.4))
Single Label:
p + geom_text(data = ann_text,label = "Text")
Multiple Labels:
p + geom_text(data = ann_text,label = "Text") + geom_text(data = dfl, mpg = 30,wt = 5, aes(label = r))
The method I'm using is trying to recreate other examples I've found here on SO and elsewhere but I seem to be missing something.
-
How to stop pathlib.Path().stat() from resolving a symlink?
I want to test modification time of a file I have:
$ /tmp/bla -> bla.1 # was linked once $ /tmp/bla.1 # was modified after the link
Now, if I run the shell command
stat bla
, I get one time, andstat bla.1
will give me a later time (because the link indeed was created once, but the destination was modified after that)When doing it from python, it returns the same values for both the link and the destination because for some reason, it resolves the link:
In [1]: from pathlib import Path In [2]: Path("bla").stat().st_mtime == Path("bla.1").stat().st_mtime Out[2]: True
This is clearly wrong:
$ stat bla | grep "^Mod" Modify: 2022-04-25 19:37:23.697182669 +0300 $ stat bla.1 | grep "^Mod" Modify: 2022-04-25 19:37:59.437309050 +0300
pathlib.Path.stat
says that:Return the result of the stat() system call on this path, like os.stat() does.
but it doesn't help me because it also resolves the link. How can this be fixed? (Same issue with
os.stat
)Python3.6.9
-
Invalid Modification Date for Linux Directories in stat C
I was working on a shell project, where I encountered a problem to which I can't find a solution to. When using stat() to get modification time, I was getting weird times with Linux directories like
/home
,/mnt
, etc.Code:
DIR *d; struct dirent *dir; struct stat s; struct tm date; char second[4]; char minute[4]; char hour[4]; char day[4]; char month[4]; char* dateStr; d = opendir("/home"); if (d) { printf("Directory Contents of \"%s\":\n\n", directory); // Printing Directory Contents message printf("%7s %16s Name\n", "Size", "Last Modified"); printf("==================================================\n"); while ((dir = readdir(d)) != NULL) { stat(dir->d_name, &s); date = *(localtime(&s.st_mtime)); if (date.tm_sec < 10) { sprintf(second, "0%d", date.tm_sec); } else { sprintf(second, "%d", date.tm_sec); } if (date.tm_min < 10) { sprintf(minute, "0%d", date.tm_min); } else { sprintf(minute, "%d", date.tm_min); } if (date.tm_hour < 10) { sprintf(hour, "0%d", date.tm_hour); } else { sprintf(hour, "%d", date.tm_hour); } if (date.tm_mday < 10) { sprintf(day, "0%d", date.tm_mday); } else { sprintf(day, "%d", date.tm_mday); } if (date.tm_mon < 10) { sprintf(month, "0%d", date.tm_mon + 1); } else { sprintf(month, "%d", date.tm_mon + 1); } if (dir->d_type == 4 || dir->d_type == 8 || dir->d_type == 10) { if (s.st_size < 1000) { printf(" <DIR> %2s/%2s/%d %2s:%2s:%2s %s\n", day, month, (date.tm_year + 1900), hour, minute, second, dir->d_name); } else if (s.st_size > 1024 && s.st_size < 1048576) { printf(" <DIR> %2s/%2s/%d %2s:%2s:%2s %s\n", day, month, (date.tm_year + 1900), hour, minute, second, dir->d_name); } else if (s.st_size > 1048576 && s.st_size < 1073741824) { printf(" <DIR> %2s/%2s/%d %2s:%2s:%2s %s\n", day, month, (date.tm_year + 1900), hour, minute, second, dir->d_name); } else if (s.st_size > 1073741824 && s.st_size < 1099511627776) { printf(" <DIR> %2s/%2s/%d %2s:%2s:%2s %s\n", day, month, (date.tm_year + 1900), hour, minute, second, dir->d_name); } else { printf(" <DIR> %2s/%2s/%d %2s:%2s:%2s %s\n", day, month, (date.tm_year + 1900), hour, minute, second, dir->d_name); } } } }
Output:
Directory Contents of "/": Size Last Modified Name ================================================== <DIR> 24/08/2975165 08:56:24 home <DIR> 24/08/2975165 08:56:24 srv <DIR> 24/08/2975165 08:56:24 etc <DIR> 24/08/2975165 08:56:24 opt <DIR> 24/08/2975165 08:56:24 root <DIR> 24/08/2975165 08:56:24 lib <DIR> 24/08/2975165 08:56:24 mnt <DIR> 24/08/2975165 08:56:24 usr <DIR> 24/08/2975165 08:56:24 media <DIR> 24/08/2975165 08:56:24 lib64 <DIR> 24/08/2975165 08:56:24 sys <DIR> 24/08/2975165 08:56:24 dev <DIR> 24/08/2975165 08:56:24 sbin <DIR> 24/08/2975165 08:56:24 boot <DIR> 24/08/2975165 08:56:24 bin <DIR> 24/08/2975165 08:56:24 run <DIR> 24/08/2975165 08:56:24 lib32 <DIR> 24/08/2975165 08:56:24 libx32 <DIR> 24/08/2975165 08:56:24 init <DIR> 24/08/2975165 08:56:24 proc <DIR> 24/08/2975165 08:56:24 snap <DIR> 24/08/2975165 08:56:24 tmp <DIR> 24/08/2975165 08:56:24 var <DIR> 24/08/2975165 08:56:24 lost+found <DIR> 16/04/2022 18:02:15 .. <DIR> 21/04/2022 17:07:47 .
The output should be something like
03/04/2022 16:03:22
(it is correct for.
and..
), so where I am making a mistake?I am sorry if my code is not clean (because I am a beginner) or I have given some unnecessary parts of the code.
-
Histogram using scale_x_continuous() to start from 0
I'm trying to create a histogram with tick marks at each bar. However, when I use
scale_x_continuous()
, the x-axis adjusted to the lowest number, which happens to be not 0. How can I make the x-axis start from 0? I triedx_lim(0,20)
but it got over-ruled byscale_x_continuous()
.set.seed(1234) dat <- as_tibble(sample(20, 100, replace = TRUE)) %>% filter(value > 5) ggplot(dat, aes(value)) + geom_histogram(color = "white", fill = "dark grey", binwidth = 1, boundary = -0.5) + scale_x_continuous(breaks = seq(0, 20, 1))
-
Remove the gap and avoid overlapping xaxis and yaxis ticklabels
I'm developing a visualization chart:
But as you can see: 1 - It has a gap at the start and end of the chart 2 - The tick labels are overlapping
I tried a lot to fix it but I can't find references and I don't know more about what I should try... Does someone know how to fix this properly?
Below is a reproducible code, any help will be really appreciated:
import pandas as pd import plotly.express as px data_dict = [{'variable': 'opt1', 'time_seconds': 62.42, 'values': 1.2506616294386024}, {'variable': 'opt1', 'time_seconds': 368.64, 'values': 1.026396270065788}, {'variable': 'opt1', 'time_seconds': 672.04, 'values': 0.9193268790432114}, {'variable': 'opt1', 'time_seconds': 967.76, 'values': 1.0040146519632747}, {'variable': 'opt1', 'time_seconds': 1319.24, 'values': 0.9758039569410012}, {'variable': 'opt1', 'time_seconds': 1621.84, 'values': 0.9608018775714326}, {'variable': 'opt2', 'time_seconds': 62.42, 'values': 53.669690262026634}, {'variable': 'opt2', 'time_seconds': 368.64, 'values': 67.29353920559024}, {'variable': 'opt2', 'time_seconds': 672.04, 'values': 82.30782533848364}, {'variable': 'opt2', 'time_seconds': 1017.26, 'values': 64.92250125677477}, {'variable': 'opt2', 'time_seconds': 1319.24, 'values': 61.70492445574225}, {'variable': 'opt2', 'time_seconds': 1621.84, 'values': 66.73124984237081}, {'variable': 'opt3', 'time_seconds': 62.34, 'values': 67.07091129789107}, {'variable': 'opt3', 'time_seconds': 364.74, 'values': 60.39192699523444}, {'variable': 'opt3', 'time_seconds': 666.68, 'values': 57.13104540996532}, {'variable': 'opt3', 'time_seconds': 967.76, 'values': 50.293945860615096}, {'variable': 'opt3', 'time_seconds': 1317.33, 'values': 73.49109300734065}, {'variable': 'opt3', 'time_seconds': 1619.03, 'values': 80.53859104682748}] table = pd.DataFrame.from_dict(data_dict) px.scatter( table, x=pd.to_datetime(table['time_seconds'],unit='s'), y="values", color="variable", hover_data=["time_seconds"], color_discrete_map={ "opt1": "#008aff", "opt2": "#8c2eff", "opt3": "#56cb32", }, ).update_traces(mode="lines+markers").for_each_trace( lambda t: t.update(yaxis="y2") if t.name == "opt1" else t ).update_layout( yaxis2={"overlaying": "y", "side": "right", "dtick":1}, xaxis_tickformat="%H:%M:%S", showlegend=False, title_text=None, )
Regards,
Leonardo -
Multiple line plotting in one graph - how to automatically adjust the x- and y-axis to find all graphs
Following from my previous question here R: Select vector (numeric) from data frame, sample n=10 subsets of size i=5 and i= 10 within vector and calculate mean for each of these samples
I want to plot the distribution of the resulting 2 vectors. I am using epdfPlot to create one density and then the second one adding it to the plot, however, I have the problem that the xlim and ylim are adjusted for graph I plot first but not for the graph(s) added.
epdfPlot(as.numeric(unlist(vector1)), curve.fill = TRUE, curve.fill.col = rgb(1, 0, 0, alpha = 0.5), epdf.col = "red", ) epdfPlot(as.numeric(unlist(IRR_mean_diversified)), curve.fill = TRUE, curve.fill.col = rgb(0, 0, 1, alpha = 0.5), epdf.col = "blue", add = TRUE)
I tried with min/max function of the values, however, that truncates the graphs.
Thank you!