I use two variables from a dataset:
I got the graph for one of them:
barchart(data_derm$PP_SD_ARBVLZ=="1")
I want to put the graph for this variable data_derm$PP_SD_ARBTLZ=="1" next to the other graph to one graph
thanks a lot for any help!
Without a reproducible example, this question will be difficult to answer definitively. One possibility is to use something like ?layout:
layout(matrix(1:2, nrow=1))
barchart(data_derm$PP_SD_ARBVLZ=="1")
barchart(data_derm$PP_SD_ARBTLZ=="1")
See the Quick-R website for more.
So you want two graphs in one figure if I am understanding you correctly.
If you want the commands from the base package. You can use:
> data(mtcars)
# par(mfrow=c(row, col)) and "row" is the number of plots you put in a row and same for "col"
> par(mfrow=c(1,2))
> barplot(mtcars$mpg)
> boxplot(mtcars$mpg ~ as.factor(mtcars$cyl))
You can use other functions if you don't mind using with GGPLOT2.. etc.
Related
I would like to plot all histogram in my data frame. One way I tried was hist.data.frame(df) which gave me very small pictures of each. Then I tried this code:
library(datasets)
data(iris)
X<-iris[,0:3]
par(mfrow=c(2,1))
histout=apply(X,2,hist)
This time, each picture is big enough but all of them have a title like Histogram NewX[,i]. When I have so many variables, this is very unclear. Is there anyway that I can add column name to each graph? Thank you!
I recommend you use lapply and feed extra arguments to the hist function in there (main, xlab, etc.). You could also use a loop.
For instance,
library(datasets)
data(iris)
X<-iris[,0:3]
par(mfrow=c(3,1))
lapply(names(X), function(k) hist(X[[k]], main=k))
Edit: Sorry, this is essentially the same answer as given in comments. I had not seen it.
I am currently trying to plot some data and don't manage to obtain a nice result. I have a set of 51 individuals with each a specific value (Pn) and split within 14 groups. The closest thing I end up with is this kind of plot. I obtain it thanks to the simple code bellow, starting by ordering my values for the Individuals :
Individuals <- factor(Individuals,levels=Individuals[order(Pn)])
dotchart(Pn,label=Individuals,color=Groups)
The issue is that I only have 9 colors on this plot (so I lost information somehow) and I can't manage to find a way to apply manually one color per group.
I've also try to use the ggplot2 package by reading it could give nice looking things. In that case I can't manage to order properly the Individuals (the previous sorting doesn't seem to have any effect here), plus I end up with only different type of blue for the group representation which is not an efficient way to represent the information given by my data set. The plot I get is accessible here and I used the following code:
ggplot(data=gps)+geom_point(mapping=aes(x=Individuals, y=Pn, color=Groups))
I apologize if this question seems redundant but I couldn't figure a solution on my own, even following some answer given to others...
Thank you in advance!
EDIT: Using the RColorBrewer as suggested bellow sorted out the issue with the colors when I use the ggplot2 package.
I believe you are looking for the scale_color_manual() function within ggplot2. You didn't provide a reproducible example, but try something along the lines of this:
ggplot(data=gps, mapping=aes(x=Individuals, y=Pn, color=Groups))+
geom_point() +
scale_color_manual(values = c('GROUP1' = 'color_value_1',
'GROUP2' = 'color_value_2',
'GROUP3' = 'color_value_3'))
Replace GROUPX with the values inside your Group column, and replace color_value_x with whatever colors you want to use.
A good resource for further learning about ggplot2 is chapter 3 of R For Data Science, which you can read here: http://r4ds.had.co.nz/data-visualisation.html
I can't be sure without looking at your data, but it looks like Groups may be a numeric value. Try this:
gps$Groups <- as.factor(gps$Groups)
library(RColorBrewer)
ggplot(data=gps)+
geom_point(mapping=aes(x=Individuals, y=Pn, color=Groups))+
scale_colour_brewer(palette = "Set1")
I searched and didn't find my solution yet. I'd like to have multiple lines to joint all boxplot's medians. I saw a lot of example, but it only worked for one set of data (for example: https://digibio.blogspot.com/2016/09/box-plots-and-connect-by-median.html). I want the graph looks as the image below, but if I use this command "stat_summary(fun.y=median, geom="line", aes(group=1)". It only plots one line. I don't know how to plot for multiple lines. Thank you.
I have the following plot:
plot.ts(returns)
I have another dataframe ma_sd which contains the rolling SD from moving averages of the above returns. The df is structured exactly like returns. Is there a simple way to add each line to the corresponding plots?
lines(1:N, ma_sd) seemed intuitive, but it does not work.
Thanks
The only way I can see you doing this is to plot them separately. This code is a bit clunky but will allow you full flexibility to be able to specify labels and axis ranges. You can build on this.
par(mfrow=c(3,1),oma=c(5,4,4,2),mar=c(0,0,0,0))
time<-as.data.frame(matrix(c(1:length(returns[,1])),length(returns[,1]),3))
plot(time[,1],returns[,1],type='l',xaxt='n')
points(time[,1],ma_sd[,1],type='l',col='red')
plot(time[,2],returns[,2],type='l',xaxt='n')
points(time[,2],ma_sd[,2],type='l',col='red')
plot(time[,3],returns[,3],type='l')
points(time[,3],ma_sd[,3],type='l',col='red')
I have a dataset as follows:
(10,75)
(20,80)
(50,85)
(100,92)
How to plot a bar-graph in R? I saw many examples in the net but none of them conform to this simple circumstance. Thanks
Try this:
data1=rbind(c(10,20,50,100),c(75,80,85,92))
barplot(data1, beside=TRUE, col=c("blue", "red"))
As an alternative, you can always use the ggplot2 library. Because of the way the data is shaped, you should also use the reshape2 library to differentiate between variables. It's a bit more complicated in this case, but in general you'll get nicer-looking barplots.
library(ggplot2)
library(reshape2)
#id variable tells what row number is used
data1=as.data.frame(cbind(id=1:4,var1=c(10,20,50,100),var2=c(75,80,85,92)))
#melt will create a row for each variable of each row, except it saves the id as a separate variable that's on every row
data1=melt(data1,id.vars='id')
#ggplot tells what data set is used and which variables do what
#geom_bar tells what type of plot should be used and certain options for the plot
ggplot(data1,aes(x=id,y=value,fill=variable))+geom_bar(stat='identity',position='dodge')