ggplot2 - add elements programmatically [closed] - r

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have built a wrapper for ggplot, and would like to enable the user of this to turn on or off certain elements. For example, in some plots the user will want to set the y-axis minimum to 0, while in others they will not.
What I want to be able to do, in this case, is have the user pass the 'ymin' variable to the function as T or F. If T, then the following element should be added to the chart.
+ expand_limits(y=0)
Is there a simple way to achieve this? Or is there an entirely different approach to this kind of problem that I should be using?

mathematical.coffee has pushed me in the right direction. Here is an elaborated answer.
plot <- ggplot(data=data, aes(...))+
geom_line()
if(ymin == T)
{
plot <- plot + expand_limits(y=0)
}

You could wrapp the your ggplot in a function, where you parameter are optionally. If no parameter are provided to the function, the assigned value in the signature is used
plotWithLimits <- function(y=0, data, ...) {
return ggplot(data=data, aes(..)) + geom_line()+expand_limits(y=0)
}

Related

Is it possible to change axis names in ggroc? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am plotting multiple ROC-curves with ggroc, and would like the axis names to be "True positive rate" and "False positive rate", rather than sens and spec. Is it possible to do this with ggroc?
I have already tried the following which didn't work:
library(pROC)
ROC_curves <- ggroc(list(log=ROC_log, tree=ROC_tree, rf=ROC_rf), aes(TPR, FPR), legacy.axes=TRUE)
I have also tried this:
ROC_curves <- ggroc(list(log=ROC_log, tree=ROC_tree, rf=ROC_rf), legacy.axes=TRUE) + scale_x(name="FPR") + scale_y(name="TPR)
ggroc returns a standard ggplot object, so you can change the axis labels and everything exactly as you would for a standard ggplot:
ggroc(list(log=ROC_log, tree=ROC_tree, rf=ROC_rf), legacy.axes=TRUE)
ROC_curves + xlab("FPR") + ylab("TPR")
Note that you are using legacy.axes=TRUE so that you can re-label the axis this way. Make sure to keep this argument, otherwise the labeling would be invalid.
Also you can drop the aes(TPR, FPR) which is silently ignored.

Adding my logs (values) to a dataframe in R [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Okay so I'm making logs of some distance variables
- example;
loghospital=log(hospital_2015_distance, base=exp(1))
Works, i get values that i can run in a regression.
However for my LASSO regression it's better i specify a dataset.
So i want a dataframe of these logs (values).
Or better I want these logs (values) added to my existing dataframe called (data).
Any idea how this can be achieved? And if not, what else i should do to achieve the same?
To add it to your data.frame you can use $:
data$loghospital = log(hospital_2015_distance, base=exp(1))
Also you could use [[ or [ and probably should <- instead of = for assignment:
# Examples:
data[["loghospital"]] <- log(hospital_2015_distance, base=exp(1))
data["loghospital"] <- log(hospital_2015_distance, base=exp(1))
data[, "loghospital"] <- log(hospital_2015_distance, base=exp(1))

Can I group * things from this data? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I got data with ASCII form.
I ran it with R, and these data have * marked when it is under other condition.
enter image description here
V1, V2, V3, V4, V5 don't mean anything different. All that matters is to classify between *-ed things.
I tried c(V1,V2,V3,V4,V5) but it returns only the levels.
I have no idea. Help me with it.
Question. Can I specify *-ed things via some code?
Is there a way to make these columned things in one data?
Select the values marked with *. I guess these values come with the symbol from the original file, right?
In this case use:
position <- grep('\\*', as.matrix(distress[]))
selectedValues <- as.matrix(distress[])[position]
numericValues <- as.numeric(gsub('\*', '', selectedValues))

Plot non-linear data points with line in R [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I simulate some data with R code and want to plot these data.
scatter plot is no problem with code plot(SX,Quant),
but when I use plot(SX,Quant,type='l'), the result is
I just want the line through every point.
I try different type of plot, also use ggplot2 to try to modification,
but has same result, plz help me to solve this problem.
Sort your data by the x-value of your plot:
F_inverse=function(x) log((exp(1)-1)*x+1)
U = runif(100)
SX = F_inverse(U)
Quant=rank(SX)/100
ord <- order(SX)
SX <- SX[ord]
Quant <- Quant[ord]
plot(SX,Quant, type="l")

How to create timeseriesgraph for multiple exposures and events in R? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to create a time-line graph (with ggplot function?) in R for 276 cases separately but displayed in one figure. They are exposed to different treatments for different times and can have multiple events. I want to mark exposure to different treatments by using colors and mark events during these treatment strategies in this graph (e.g. with dots).
my data look like this
Does someone know (partly) how to do this?
Your data is not in what is called 'Tidy format'. You first want to do some data wrangling to get your data from 'broad' to 'long' format before you get to plotting your data. Your different treatments should be in a single column (called treatments) instead of multiple columns. I suggest you look into the dplyr and tidyr packages (see this cheatsheet).
To put your treatments into rows rather than columns you could use the gather function from the tidyr package. Once this is done you could create a ggplot similar to:
ggplot(your_data, aes(x = time_of_event, y = value, colour = treatment)) +
geom_point()

Resources