I have csv files that each has multiple stack plot, but input csv files named with rather long character. However, in my resulted plot, full name of csv files not fully printed in facet_wrap which easily confused category of plot are referring to. I am trying to adjust the size of face_wrap by using space, scale parameter, but full name of input csv files still is not displayed. Can anyone point me how to deal with csv files with rather long pattern that must be displayed in resulted plot ? How can I make this happen ? Any idea ?
I have csv file which named with rather long character (just toy example here) :
TextTextTextTextTextTextTextTextTextTextTextTextTextText.csv
This is my resulted plot and desired plot that I want to achieve (here I showed multiple stack plot for only one csv file) :
I intend to continue the code part of my original plot. How can I get my desired plot ? Any way to tune size of face_wrap where rather long named file can be displayed in resulted plot ? Thanks a lot :)
Edit :
I want to adjust the space between multiple stack plot for each csv file as well. If multiple plot of two or three csv files are placed in one single page, How to dynamically adjust space, and plot size that make sure rather long named csv files are more readable. How can I achieve this ? Any idea please ?
New Edit :
Qualified <- list(
hotankarmaykuchakorla = data.frame( begin=seq(1, by=6, len=20), end=seq(4, by=6, len=20), pos.score=sample(30, 20)),
aksukexkerawataltay = data.frame( begin=seq(3, by=9, len=15), end=seq(6, by=9, len=15), pos.score=sample(28, 15))
)
UnQualified <- list(
hotankarmaykuchakorla = data.frame( begin=seq(9, by=12, len=30), end=seq(14, by=12, len=30), pos.score=sample(35, 30)),
aksukexkerawataltay = data.frame( begin=seq(13, by=10, len=20), end=seq(19, by=10, len=20), pos.score=sample(34, 20))
)
get multiple stack plot for this:
hotankarmaykuchakorla.validCandidate.Qualified.csv
hotankarmaykuchakorla.validCandidate.unQualified.csv
hotankarmaykuchakorla.invalidCandidate.Qualified.csv
hotankarmaykuchakorla.invalidCandidate.UnQualified.csv
Maybe using newline:
library(ggplot2)
# dummy data
df1 <- mtcars[, 1:3]
# make new long name
df1$cyl <- paste0("TextTextTextTextTextTextTextTextTextText", df1$cyl, ".csv")
# add newline
df1$cylWrap <-
paste0(substr(df1$cyl, 1, 20), "\n",
substr(df1$cyl, 21, nchar(df1$cyl)))
# plot
ggplot(df1, aes(mpg, disp)) +
geom_col() +
facet_wrap( ~cylWrap, scales = "free_x") +
theme(text = element_text(size = 14))
Related
I'm new to R and I'm trying to plot a data frame of county values using the usmap library.
I have a dataset containing all the FIPS (county codes) for a particular region and the data (deaths) that I want to show on the map.
This is my first R script so what I'm trying to accomplish is likely pretty easy and I just have no idea what I'm doing.
I'm pretty sure the error I'm receiving is because I haven't specified any kind of coloring to apply to the data? I'm unsure.
Here's my code - note that I'm trying to initially just plot one frame of data (a particular date):
library(usmap)
library(ggplot2)
library(RColorBrewer)
#set working directory
setwd("C:/Users/Name/Documents/RScripts/")
#input data from file separated by commas
usa.dat <- read.csv("covid.csv", header = T)
#Start at 1/22/2020
#End at 10/8/2021
plot_usmap(regions = "counties",
data=usa.dat$countyFIPS,
values=usa.dat$X1.22.2020,
) +
theme(panel.background = element_rect(color = "black", fill = "black"))
Here's the data:
The error I'm getting is Error in if (is.null(geom_args[["fill"]]) & nrow(data) == 0) { : argument is of length zero
When I remove the data/values lines from the function, I get a map that looks like this:
Any help is greatly appreciated!
Ideally, I'd like to animate each frame of the data with color scales; if you guys can help me with that, I'd appreciate it!
Edit:
Okay so, I've been working on this for awhile and I managed to get it working. I have no idea how to update the color scales/gradients that are used, however.
I got it to loop through the data and save a bunch of plots, so that's pretty awesome! Just need to figure out how to change the colors/scales if anyone can help!
I got it figured out (with some help from Ben!)
library(usmap)
library(ggplot2)
library(viridis)
library(RColorBrewer)
library(stringr)
library(stringi)
#set working directory
setwd("C:/Users/Tyrael/Documents/RScripts/")
#input data from file separated by commas
usa.dat <- read.csv("ccovid.csv", header = T)
for (i in 2:ncol(usa.dat)) {
da <- data.frame(fips=usa.dat$countyFIPS,val=usa.dat[,i])
da$val <- cut(da$val,breaks=c(0,100,500,1000,5000,20000,30000),labels=c("1-100","100-500","500-1K","1K-5K","5K-20K","20K-30K"))
theDate <- substr(names(usa.dat)[i],2,100)
plot_usmap(regions = "counties",
data=da,
values="val"
) +
labs(title=paste("Covid-19 Deaths ",str_replace_all(theDate,"\\.","/"),sep='')) +
scale_fill_viridis(name="Deaths",discrete=TRUE,na.translate=F) +
theme(panel.background = element_rect(color = "#101010", fill = "#101010"))
ggsave(paste(sprintf("%03d",i),".png",sep=''))
}
This splits everything up into a legend that looks like this:
The files are output in sequential order and, as a bonus, I'll show how to combine in ffmpeg:
ffmpeg -framerate 15 -i "C:\Users\Tyrael\Documents\RScripts\vpublish\%03d.png" -codec copy out.mkv
And to get it into .mp4 format:
ffmpeg -i out.mkv -codec copy out.mp4
Results: https://www.youtube.com/watch?v=-z3LL5j__es
I've been trying to save multiple plot generated with the meta package in R, used to conduct meta-analysis, but I have some troubles. I need to save this plot to arrange them in a multiple plot figure.
Example data:
s <- data.frame(Study = paste0("Study", 1:15),
event.e = sample(1:100, 15),
n.e = sample(100:300, 15))
meta1 <- meta::metaprop(event = event.e,
n= n.e,
data=s,
studlab = Study)
Here is the code:
meta::funnel(meta1)
funnelplot <- grid::grid.grab()
I can see the figure in the "plot" tab in R Studio; However, if I search the funnelplot object in the environment it say that is a "NULL" type, and obviously trying to recall that doesn't work.
How can I fix it?
My sample dataset:
library(plotly)
library(htmlwidgets)
d <-
data.frame(
"name" = c("bily", "mary", "ken"),
"fruit" = c("cherry", "apple", "apple"),
"count" = c(1, 10, 30)
)
gg <- ggplot(data = d,
aes(x = fruit,
y = count,
group = name)) +
geom_line(aes(color = name)) +
geom_point(aes(color = name))
plotly <- ggplotly(gg)
save graph:
d <- # please put your directory where you want to save the graph
"xxxx/graph.html"
I have searched these three functions that can output the HTML format but they will also make other files and I cannot solve it.
#htmlwidgets::saveWidget(as_widget(plotly), d)
#htmltools::browsable(plotly)
htmltools::save_html(plotly, d)
I am trying to convert my graph, which uses from ggplot function to ggplotly function, to HTML format but I face a problem. When I save the graph, the function will also make other files such as plotlyjs, jquery, and crosstalk. I hope only the HTML file can be made because it is too hard to put the files on the website if each graph has different files.
These are the files when I run the code. My expected result is that only the HTML file is made.
The other files are saved in the directory of the lib but I hope they are not provided and I can run the HTML file. Is it possible to do? or Are there any other methods?
Just add selfcontained = TRUE to your last line:
htmlwidgets::saveWidget(as_widget(plotly), selfcontained = TRUE, file = "xxxx/graph.html")
I am going to plot a boxplot from a 4-column matrix pl1 using ggplot with dots on each box. The instruction for plotting is like this:
p1 <- ggplot(pl1, aes(x=factor(Edge_n), y=get(make.names(y_label)), ymax=max(get(make.names(y_label)))*1.05))+
geom_boxplot(aes(fill=method), outlier.shape= NA)+
theme(text = element_text(size=20), aspect.ratio=1)+
xlab("Number of edges")+
ylab(y_label)+
scale_fill_manual(values=color_box)+
geom_point(aes(x=factor(Edge_n), y=get(make.names(true_des)), ymax=max(get(make.names(true_des)))*1.05, color=method),
position = position_dodge(width=0.75))+
scale_color_manual(values=color_pnt)
Then, I use print(p1) to print it on an opened pdf. However, this does not work for me and I get the below error:
Error in make.names(true_des) : object 'true_des' not found
Does anyone can help?
Your example is not very clear because you give a call but you don't show the values of your variables so it's really hard to figure out what you're trying to do (for instance, is method the name of a column in the data frame pl1, or is it a variable (and if it's a variable, what is its type? string? name?)).
Nonetheless, here's an example that should help set you on the way to doing what you want:
Try something like this:
pl1 <- data.frame(Edge_n = sample(5, 20, TRUE), foo = rnorm(20), bar = rnorm(20))
y_label <- 'foo'
ax <- do.call(aes, list(
x=quote(factor(Edge_n)),
y=as.name(y_label),
ymax = substitute(max(y)*1.05, list(y=as.name(y_label)))))
p1 <- ggplot(pl1) + geom_boxplot(ax)
print(p1)
This should get you started to figuring out the rest of what you're trying to do.
Alternately (a different interpretation of your question) is that you may be running into a problem with the environment in which aes evaluates its arguments. See https://github.com/hadley/ggplot2/issues/743 for details. If this is the issue, then the answer might to override the default value of the environment argument to aes, for instance: aes(x=factor(Edge_n), y=get(make.names(y_label)), ymax=max(get(make.names(y_label)))*1.05, environment=environment())
I've been trying to write out an R script that will plot the date-temp series for a set of locations that are identified by a Deployment_ID.
Ideally, each page of the output pdf would have the name of the Deployment_ID (check), a graph with proper axes (check) and correct scaling of the x-axis to best show the date-temp series for that specific Deployment_ID (not check).
At the moment, the script makes a pdf that shows each ID over the full range of the dates in the date column (i.e. 1988-2010), instead of just the relevant dates (i.e. just 2005), which squishes the scatterplot down into uselessness.
I'm pretty sure it's something to do with how you define xlim, but I can't figure out how to have R access the date min and the date max for each factor as it draws the plots.
Script I have so far:
#Get CSV to read data from, change the file path and name
data <- read.csv(file.path("C:\Users\Person\Desktop\", "SampleData.csv"))
#Make Date real date - must be in yyyy/mm/dd format from the csv to do so
data$Date <- as.Date(data$Date)
#Call lattice to library, note to install.packages(lattice) if you don't have it
library(lattice)
#Make the plots with lattice, this takes a while.
dataplot <- xyplot(data$Temp~data$Date|factor(data$Deployment_ID),
data=data,
stack = TRUE,
auto.key = list(space = "right"),
layout = c(1,1),
ylim = c(-10,40)
)
#make the pdf
pdf("Dataplots_SampleData.pdf", onefile = TRUE)
#print to the pdf? Not really sure how this works. Takes a while.
print(dataplot)
dev.off()
Use the scales argument. give this a try
dataplot <- xyplot(data$Temp~data$Date|factor(data$Deployment_ID),
data=data,
stack = TRUE,
auto.key = list(space = "right"),
layout = c(1,1),
scales= list( relation ="free")
)