How to use a variable to specify column name in ggplot
You can use aes_string
:
f <- function( column ) {
...
ggplot( rates.by.groups, aes_string(x="name", y="rate", colour= column,
group=column ) )
}
as long as you pass the column to the function as a string (f("majr")
rather than f(majr)
). Also note that we changed the other columns, "name"
and "rate"
, to be strings.
If for whatever reason you'd rather not use aes_string
, you could change it to (the somewhat more cumbersome):
ggplot( rates.by.groups, aes(x=name, y=rate, colour= get(column),
group=get(column) ) )
Pass/loop over column names to data.table and ggplot as variables
try to use get(ZZZ)
in loop body, instead of ZZZ
Refering a column by its name in ggplot in a loop
We can use aes_string
to pass strings
l1 <- vector("list", length(var.names))
for(v in seq_along(var.names)){
variable <- var.names[v]
l1[[v]] <- ggplot(df, aes_string(x = variable)) +
geom_histogram() +
facet_wrap(~id)
}
Or another option in the dev version should be to convert the string to symbol (rlang::sym
) and evaluate (!!
) within the aes
for(v in seq_along(var.names)){
variable <- rlang::sym(var.names[v])
l1[[v]] <- ggplot(df, aes(x = !!variable)) +
geom_histogram() +
facet_wrap(~id)
}
The plots stored in the list
can be saved in a .pdf
file
library(gridExtra)
l2 <- map(l1, ggplotGrob)
ggsave(marrangeGrob(grobs = l2, nrow = 1, ncol = 1), file = 'plots.pdf')
If we need to overlay the three plots in a single page, use gather
to convert to 'long' format
library(tidyr)
library(dplyr)
gather(df, key, val, var.names) %>%
ggplot(., aes(x = val, fill = key)) +
geom_histogram() +
facet_wrap(~id)
-output
ggplot2 Variable Name with Spaces and tidyeval Syntax
Because it's quoted text, instead of enquo
, use rlang::sym
xVariable <- "Month"
yVariable <- "High Temperature"
fun <- function(dat, xvar, yvar) {
xvar <- rlang::sym(xvar)
yvar <- rlang::sym(yvar)
p1 <-dat %>%
ggplot(aes(x = !!xvar, y = !!yvar)) +
geom_point()
return(p1)
}
fun(weather, xVariable, yVariable)
Variable name as argument in function - ggplot R
If you call plot_var_aantal
with a string like "people"
you need to evalute vari
as a symbol on the righthand side with !!sym(vari)
and on the lefthand side you need to put it into a glue
specification "{vari}"
. The following code should work:
plot_var_aantal<-function(vari){
eval(substitute(vari), MASTERDATA)
MASTERDATA%>%
group_by(COMPANY)%>%
summarize(AMOUNT_COMP = (sum(AMOUNT, na.rm=TRUE)),
Type=Type,
"{vari}" := !!sym(vari)) %>% # changed this line
filter(!is.na(Type)) %>%
ggplot(aes(!! sym(vari),AMOUNT_COMP ), na.rm = TRUE) + # changed this line
geom_point()+
facet_wrap(~Type,nrow=4)
}
However, without seeing your data it is hard to figure out what COMP_VAR1 = COMP_VAR1
is doing in your dplyr::summarise
call. You are not using an aggregating function (like mean
or paste(..., collapse = ",")
) so the whole summarise
is probably not summarising but returning data in the original length. Similarly the line "{vari}" := !!sym(vari)
doesn't seem to make sense (although the non-standard evaluation when vari
is a string, is correct).
R pass variable column indices to ggplot2
You can use the aes_string
in stead of aes
to pass string in stead of using objects, i.e.:
myplot = function(df, x_string, y_string) {
ggplot(df, aes_string(x = x_string, y = y_string)) + geom_point()
}
myplot(df, "A", "B")
myplot(df, "B", "A")
ggplot and dplyr and column name as string
You need to UQ
(or !!
) the name/symbol. For example
for(el in name_list){
pdf(paste(el, '.pdf', sep =''))
test <- my_df %>% group_by(UQ(as.name(el))) %>% summarize(count = n())
print(ggplot(data = test, aes_string(x = el, y = 'count')) + geom_bar(stat='identity'))
dev.off()
}
Use ggplot in an R function with three inputs: filename of dataframe, and two column variables of numeric data
df <- data.frame(
"choco" = 1:5,
"tea" = c(2, 4, 5, 8, 10),
"coffee" = c(0.5, 2, 3, 1.5, 2.5),
"sugar" = 16:20
)
test <- function(Data, ing1, ing2) {
if (ing1 %in% names(Data) & ing2 %in% names(Data)) {
ggplot(Data, aes(x = Data[, ing1], y = Data[, ing2])) +
geom_point()
}
else {
print("Both ing1, and ing2 has to be columns of data frame")
}
}
test(Data = df, ing1 = "choco", ing2 = "sugar")
Regards,
Grzegorz
Is it possible to make a column plot using ggplot in which the column fill is controlled by a third variable?
It looks like you have 3 X values that each appear 5, 10, or 15 times. Do you want the bars to be overlaid on top of one another, as they are now? If you add an alpha = 0.5 to the geom_col call you'll see the overlapping bars.
Alternatively, you might use dodging to show the bars next to one another instead of on top of one another.
ggplot(df, aes(x=x, y=y, fill=z, group = z)) +
geom_col(width=0.5, position=position_dodge()) +
scale_fill_viridis_c(option="turbo", # added with ggplot 3.x in 2018
limits = c(0,10),
breaks=seq(0,10,2.5),
labels=c("0","2.5","5.0","7.5","10.0"))
Or you might plot the data in order of y
so that the smaller bars appear on top, visibly:
ggplot(dplyr::arrange(df,y), aes(x=x, y=y, fill=z))+
geom_col(width=0.5, position="identity") +
scale_fill_viridis_c(option="turbo",
limits = c(0,10),
breaks=seq(0,10,2.5),
labels=c("0","2.5","5.0","7.5","10.0"))
Related Topics
Calculate Max Value Across Multiple Columns by Multiple Groups
Calculate Difference Between Values in Consecutive Rows by Group
How to Replace Negative Values in a Dataframe Column With a Different Value
Using Ifelse Statement on the Whole Dataset Instead of a Single Column
Replacing Na Values from Another Dataframe by Id
Count Number of Rows Per Group and Add Result to Original Data Frame
Simultaneously Merge Multiple Data.Frames in a List
Finding All Duplicate Rows, Including "Elements With Smaller Subscripts"
How to Sort a Character Vector Where Elements Contain Letters and Numbers
Is the "*Apply" Family Really Not Vectorized
Is There a Dplyr Equivalent to Data.Table::Rleid
Multirow Axis Labels With Nested Grouping Variables
General Suggestions For Debugging in R
Sum Rows in Data.Frame or Matrix