How can I create an image from a data frame? For example:
library(tidyverse)
library(gridExtra)
df = iris %>% slice(1:4)
I've the following but:
1. I haven't been able to get this to be saved to a variable. It just pops up in the plots pane of Rstudio. Am I missing something obvious? I'd like to be able to have this plot referenced to a variable so I could save it as a png or something.
2. Is there a way to remove the row numbers that seem to appear?
3. This look is fine, but is there a way to make it more of a lighter background compared to what this is?
gridExtra::grid.table(df)
To save a relevant variable, use
myTable <- tableGrob(df)
since
grid.table
# function (...)
# grid.draw(tableGrob(...))
# <bytecode: 0x10758c078>
# <environment: namespace:gridExtra>
Given that, you can run
library(grid)
grid.draw(myTable)
For that you want
myTable <- tableGrob(df, rows = NULL)
See ?tableGrob and particularly ttheme_default (its source code makes pretty clear what are possible parameters; see also here). For instance,
myTable <- tableGrob(
df,
rows = NULL,
theme = ttheme_default(core = list(bg_params = list(fill = "grey99")))
)
grid.draw(myTable)
Related
I imported information from an excel file into RStudio. When I tried to create a bar chart out of two of the set's columns it didn't work.
ggplot(data = Encuesta_BIDVOX)+geom_bar(mapping = aes(x=Cargo, fill=1_PC_Estandarizado))
The Console's output showed:
Error: unexpected input in "ggplot(data = Encuesta_BIDVOX)+geom_bar(mapping = aes(x=Cargo, fill=1_"
I realized that the issue was that the Variable's name (the column's name) which I'm using to fill the bar starts with a number (I tried with other columns with no numbers starting its name and it worked fine).
So the question is:
Is there a way to command R to ignore that the Columns name is starting with a number, and I can use it in the ggplot function without ending in an error?
This would be of immense help since most of the columns (over 48) start with a number...
Please keep answers as simple as possible since as you already noticed I'm new at R. Thanks!
Just using `` may helps(note that it's not ')
iris3 <- iris
names(iris3) <- c("Sepal.Length", "Sepal.Width" , "Petal.Length" ,"Petal.Width" , '1_')
iris3 %>%
ggplot(aes(Sepal.Length, Sepal.Width, color = `1_`)) +
geom_point()
I suggest fixing this as far upstream as possible, potentially in the source file, or upon import, or if not then before you go to ggplot. janitor::clean_names() offers a nice interface for doing that and controlling what the new, syntactical column names look like.
uhoh <- data.frame(`1_` = 1,
`2_` = 2,
`10_` = 3,
check.names = FALSE)
uhoh
# 1_ 2_ 10_
#1 1 2 3
uhoh %>%
janitor::clean_names() %>%
ggplot() +
geom_bar(aes(x1, fill = x10))
You can use make.names, which is used by e.g. data.frame() to make sure names are valid.
# make a dataframe with invalid names
x <- data.frame('1a' = 1, '2b' = 1, '_c' = 1, check.names = FALSE)
# make any invalid names valid
names(x) <- make.names(names(x), unique = TRUE)
But, it is almost always better to fix the names where you are creating them (e.g. most functions to read in data have arguments to make sure the names are checked and valid).
I am using nVennr, which has proved to be quite useful, although have been through all of the supporting documentation and still can't find out how to do 2 things that I am imagine are possible. They are how to change the displayed figures to non-standard-form format, and how to remove the legend.
Below is some data that illustrates how nVennr automatically converts figures to standard form.
library(nVennr)
library(dplyr)
library(rsvg)
library(grImport2)
SetA = round(runif(10000000),0)
SetB = round(runif(10000000),0)
SetC = round(runif(10000000),0)
allSets = cbind(SetA, SetB, SetC) %>%
as.data.frame() %>%
rownames_to_column()
SetA = subset(allSets, SetA == 1)$rowname
SetB = subset(allSets, SetB == 1)$rowname
SetC = subset(allSets, SetC == 1)$rowname
myV = plotVenn(list(SetA = SetA,
SetB = SetB,
SetC = SetC),
nCycles = 10000)
Any help would be greatly appreciated, in particular with the standard form issue, and this renders the plots quite ugly.
Thanks!
Glad you like the package. I am not sure what you mean by standard form. Perhaps you want one of those diagrams with three overlapping circles. In that case, I would recommend using other packages, like those suggested here. You can get a regular Venn diagram with nVennR, as shown in the vignette, but the default shape is always round in the region, not in the set:
library(nVennR)
myV3 <- createVennObj(nSets = 3, sSizes = c(rep(1, 8)))
myV3 <- plotVenn(nVennObj = myV3, labelRegions=F, showNumbers=F, opacity=0.2, borderWidth=4)
The result would be:
Regarding the legend, I did not think that would be necessary, as it is very easy to remove from the resulting svg file. However, since I need to upload a new version, I will add this function shortly.
EDIT:
The new version of nVennR (0.2.2) has just been released. Now, plotVenn and showSVG have the option showLegend. If set to false, the legend is not shown.
I am trying to use a for loop to create a ggplot for each column in a dataframe. I am pretty new to this so my approach may be very wrong here.
I have written a function to create the ggplot:
create_scatter <- function(df, x, y) {
ggplot(df, aes(x, y)) +
geom_point() +
xlab(name) +
ylab("quality")
}
And a for loop to iterate through the Dataframe columns by name (to get the name of the column for use later) then get the contents of the column for the plotting function.
for (name in names(whiteWines)) {
for (column in whiteWines[name]) {
assign(paste0(name, "_scatter"),
create_scatter(whiteWines, column, whiteWines$quality))
}
}
Using assign() I am able to create a variable name from the column name on the fly and assign the results of ggplot to it.
I am then using grid.arrange to arrange the resulting plots in a 3 x 4 grid.
grid.arrange(fixed.acidity_scatter,
volatile.acidity_scatter,
citric.acid_scatter,
residual.sugar_scatter,
chlorides_scatter,
free.sulfur.dioxide_scatter,
total.sulfur.dioxide_scatter,
density_scatter,
pH_scatter,
sulphates_scatter,
alcohol_scatter,
layout_matrix = rbind(c(1,2,3), c(4,5,6), c(7,8,9), c(10,11,12)))
When executed all scatter plots are created, however they all contain the data from the last scatter plot in the loop.
Undesired Results
If I wrap the assign statement in a print() statement then I do get the desired outcome in the grid, but each individual plot gets printed as well.
Desired Results
Dataset
You're probably looking for something more like this:
library(readr)
library(tidyr)
library(dplyr)
library(ggplot2)
ww <- read_delim(file = "~/Downloads/winequality-white.csv",delim = ";")
ww_long <- ww %>%
gather(key = measure,value = value,`fixed acidity`:`alcohol`)
ggplot(data = ww_long,aes(x = quality,y = value)) +
facet_wrap(~measure,scales = "free_y") +
geom_point()
R has some tools that can be very tempting for beginners as they think through solving a problem. Among them are assign(), get() and eval(parse(text = )). It is usually the case that a solution using those will cause more problems than they solve; there's typically a better way, but will require digging a little deeper into the "normal" way of doing things in R.
The followings are the variables of the data
"fixed acidity";"volatile acidity";"citric acid";"residual sugar";"chlorides";"free sulfur dioxide";"total sulfur dioxide";"density";"pH";"sulphates";"alcohol";"quality"
the followings are sample rows
7;0.27;0.36;20.7;0.045;45;170;1.001;3;0.45;8.8;6
6.3;0.3;0.34;1.6;0.049;14;132;0.994;3.3;0.49;9.5;6
8.1;0.28;0.4;6.9;0.05;30;97;0.9951;3.26;0.44;10.1;6
7.2;0.23;0.32;8.5;0.058;47;186;0.9956;3.19;0.4;9.9;6
7.2;0.23;0.32;8.5;0.058;47;186;0.9956;3.19;0.4;9.9;6
8.1;0.28;0.4;6.9;0.05;30;97;0.9951;3.26;0.44;10.1;6
6.2;0.32;0.16;7;0.045;30;136;0.9949;3.18;0.47;9.6;6
7;0.27;0.36;20.7;0.045;45;170;1.001;3;0.45;8.8;6
6.3;0.3;0.34;1.6;0.049;14;132;0.994;3.3;0.49;9.5;6
8.1;0.22;0.43;1.5;0.044;28;129;0.9938;3.22;0.45;11;6
All form the excel sheet.
I have a list, which contains 75 matrix with their names, and I want to do a plot for each matrix, and save each plot with the name that the matrix have.
My code do the plots with a loop and it works, I get 75 correct plots, but the problem is that the name of the plot file is like a vector "c(99,86,94....)",too long and I don´t know which one is.
I´m ussing that code, probably isn´t the best. I´m a beginner, and I have been looking for a solution one week, but it was impossible.
for (i in ssamblist) {
svg(paste("Corr",i,".svg", sep=""),width = 45, height = 45)
pairs(~CDWA+CDWM+HI+NGM2+TKW+YIELD10+GDD_EA,
data=i,lower.panel=panel.smooth, upper.panel=panel.cor,
pch=0, main=i)
dev.off()}
How put to a each plot his name?.
I try change "i" for names(i), but the name was the name of the first column,and only creates one plot. I try to do it with lapply but I could't.
PS: the plots are huge, and I have to expand the margins. I´m using Rstudio.
Thank you¡
Using for loop or apply:
# dummy data
ssamblist <- list(a = mtcars[1:10, 1:4], b = mtcars[11:20, 1:4], c = mtcars[21:30, 1:4])
# using for loop
for(i in names(ssamblist)) {
svg(paste0("Corr_", i, ".svg"))
pairs(ssamblist[[i]], main = i)
dev.off()}
# using apply
sapply(names(ssamblist), function(i){
svg(paste0("Corr_", i, ".svg"))
pairs(ssamblist[[i]], main = i)
dev.off()})
I see a lot of examples in javascript but I cannot find an example to do it in R
Here is the api link: http://api.highcharts.com/highcharts#global
I am trying to set "timezoneOffset" and I have tried many different ways.
When I do this in R: highChart$global(timezoneOffset=-300)
I do not get any warning or error, but it's not working.
Thanks a lot for the help!
Here is a piece of code:
library(rCharts)
highChart <- Highcharts$new()
highChart$global(timezoneOffset=-300)
highChart$chart(zoomType = "xy")
highChart$exporting(enabled = T)
highChart$xAxis(type="datetime",list( title = list(text = "Time")))
highChart$yAxis(list
(
list(title = list(text = "Variance"))
))
highChart$series(data=list(c(x=1407795845000,y=1),c(x=1407795846000,y=2),c(x=1407795847000,y=3)))
highChart
As you can see, the timezoneOffset is not working when I run this piece of code and the time is still displayed in GMT.
As of version 0.5.0 of highcharter, it seems the option highcharter.options is not there any more, but there are several separate options, e.g. highcharter.lang, highcharter.global, etc. So the following approach works:
lang <- getOption("highcharter.lang")
lang$decimalPoint <- ","
lang$numericSymbols <- highcharter::JS("null") # optional: remove the SI prefixes
options(highcharter.lang = lang)
In addition to changing the decimal point, the SI prefixes ("k", "M", "G", etc.) are turned off by setting the numericSymbols to null, see Replacing/removing the metric notations like thousands "k" abbreviation.
The highcharter options can be accessed, but they are set inside the standard R options under the list element highcharter.options. They are not given directly to the highchart, and inside highchart(), there is the code line opts <- getOption("highcharter.options", list()).
I don't think there is another way than just get the options, alter whatever options you need to change and then set the options again with your additions.
The following is a simple illustration:
library(highcharter)
# normal highchart
highchart() %>%
hc_add_serie_labels_values(1:901, seq(1, 10, 0.01))
opts <- getOption("highcharter.options")
opts$lang$decimalPoint <- "."
options(highcharter.options = opts)
# now with "," instead of "." (confirm in tooltip)
highchart() %>%
hc_add_serie_labels_values(1:901, seq(1, 10, 0.01))
Of course in your case, you need to set the $global$timezoneOffset part.
First you have to switch of the useUTC flag to FALSE. Than you can set the timezoneOffset as you wish and save the options back.
global <- getOption("highcharter.global")
global$useUTC <- FALSE
global$timezoneOffset <- -300
options(highcharter.global = global)
For better understanding make sure you take a look at global:
str(global)