I have the following data frame:
A B C D Xax 0.451 0.333 0.034 0.173 0.22 0.491 0.270 0.033 0.207 0.34 0.389 0.249 0.084 0.271 0.54 0.425 0.819 0.077 0.281 0.34 0.457 0.429 0.053 0.386 0.53 0.436 0.524 0.049 0.249 0.12 0.423 0.270 0.093 0.279 0.61 0.463 0.315 0.019 0.204 0.23
I need to plot all these columns in the same plot(on the x-axis I want the variable Xax and the y-axis the variables A,B,C and D) and also to draw the regression line for each variable alone.
I tried this:
pl<-ggplot(data=df) + geom_point(aes(x=Xax,y=A,size=10)) + geom_point(aes(x=Xax,y=B,size=10)) + geom_point(aes(x=Xax,y=C,size=10)) + geom_point(aes(x=Xax,y=D,size=10)) + geom_smooth(method = "lm", se=FALSE, color="black")
But it's only plotting the first one(Xax and A)
You can create a scatter plot in R with multiple variables, known as pairwise scatter plot or scatterplot matrix, with the pairs function. In addition, in case your dataset contains a factor variable, you can specify the variable in the col argument as follows to plot the groups with different color.
Because, usually R dataframes do not allow exact same names (when you create them using data. frame() ). Which means dataframes should not have the same column names. In your example, when you do d3$a R can only show you the first column that have the name a .
Often you may want to plot multiple columns from a data frame in R. Fortunately this is easy to do using the visualization library ggplot2. This tutorial shows how to use ggplot2 to plot multiple columns of a data frame on the same graph and on different graphs.
R par() function. We can put multiple graphs in a single plot by setting some graphical parameters with the help of par() function. R programming has a lot of graphical parameters which control the way our graphs are displayed. The par() function helps us in setting or inquiring about these parameters.
In this method, we plot a line graph for each column of the dataframe in the different panel of a same plot. We can achieve this task by adding facet_grid () function. facet_grid () function produces a layout panel defined by rows and columns.
. . . where A refers to the number of rows and B to the number of columns (and where each cell will hold a single graph). This syntax sets up a plotting environment of A rows and B columns. First we create four vectors, all of the same length.
The easiest is to convert your data to a "tall" format.
s <- "A B C G Xax 0.451 0.333 0.034 0.173 0.22 0.491 0.270 0.033 0.207 0.34 0.389 0.249 0.084 0.271 0.54 0.425 0.819 0.077 0.281 0.34 0.457 0.429 0.053 0.386 0.53 0.436 0.524 0.049 0.249 0.12 0.423 0.270 0.093 0.279 0.61 0.463 0.315 0.019 0.204 0.23 " d <- read.delim(textConnection(s), sep="") library(ggplot2) library(reshape2) d <- melt(d, id.vars="Xax") # Everything on the same plot ggplot(d, aes(Xax,value, col=variable)) + geom_point() + stat_smooth() # Separate plots ggplot(d, aes(Xax,value)) + geom_point() + stat_smooth() + facet_wrap(~variable)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With