row-wise bar plot in r [duplicate] - r
This question already has answers here:
Single barplot for each row of dataframe
(2 answers)
Closed 13 days ago.
I picked up r recently and was trying some code for data visualization. For practice, I created a small data frame to plot the data and understand the result.
First I tried plotting a simple vector, like temperature over a week, and function barplot worked like a charm.
later I moved on to plot a simple tabular data of marks of students in 2 subjects as shown below:
stuname sub1 sub2
st1 rocket 95 70
st2 Ash 58 85
I used below to create the dataframe
plotdata=data.frame("stuname"=c("rocket","Ash"),
"sub1"=c(95,58),
"sub2"=c(70,85),
row.names = c("st1","st2"))
I am using below to plot the data
barplot(as.matrix(plotdata[ ,2:3]), xlab = "Stu", ylab = "marks", beside = TRUE)
I think the requirement is basic enough so I have not moved to ggplot yet.
This is what I'm getting:
This is what I was expecting:
I mean, this is how usually we would like to plot, we can keep on adding row data and the plot can keep on increasing and I see one figure to get all the marks for a particular student.
Separate just the numeric values and transpose them so that they will plot in the order you want. Note that if you transpose without separating the numeric values, they may be converted to character.
barplot(height = t(plotdata[c("sub1", "sub2")]),
names.arg = plotdata$stuname,
beside = TRUE)
I would still recommend using ggplot as it takes care of so many things for you
library(reshape2)
library(ggplot2)
#Convert to long format
d = melt(plotdata, id.vars = "stuname")
ggplot(data = d,
mapping = aes(x = stuname, y = value, fill = variable)) +
geom_col(position = position_dodge())
Related
Graphing different variables in the same graph R- ggplot2
I have several datasets and my end goal is to do a graph out of them, with each line representing the yearly variation for the given information. I finally joined and combined my data (as it was in a per month structure) into a table that just contains the yearly means for each item I want to graph (column depicting year and subsequent rows depicting yearly variation for 4 different elements) I have one factor that is the year and 4 different variables that read yearly variations, thus I would like to graph them on the same space. I had the idea to joint the 4 columns into one by factor (collapse into one observation per row and the year or factor in the subsequent row) but seem unable to do that. My thought is that this would give a structure to my y axis. Would like some advise, and to know if my approach to the problem is effective. I am trying ggplot2 but does not seem to work without a defined (or a pre defined range) y axis. Thanks
I would suggest next approach. You have to reshape your data from wide to long as next example. In that way is possible to see all variables. As no data is provided, this solution is sketched using dummy data. Also, you can change lines to other geom you want like points: library(tidyverse) set.seed(123) #Data df <- data.frame(year=1990:2000, v1=rnorm(11,2,1), v2=rnorm(11,3,2), v3=rnorm(11,4,1), v4=rnorm(11,5,2)) #Plot df %>% pivot_longer(-year) %>% ggplot(aes(x=factor(year),y=value,group=name,color=name))+ geom_line()+ theme_bw() Output:
We could use melt from reshape2 without loading multiple other packages library(reshape2) library(ggplot2) ggplot(melt(df, id.var = 'year'), aes(x = factor(year), y = value, group = variable, color = variable)) + geom_line() -output plot Or with matplot from base R matplot(as.matrix(df[-1]), type = 'l', xaxt = 'n') data set.seed(123) df <- data.frame(year=1990:2000, v1=rnorm(11,2,1), v2=rnorm(11,3,2), v3=rnorm(11,4,1), v4=rnorm(11,5,2))
How to plot bar charts with 'n' number of columns and group by another column?
I am learning r currently and I have an r data-frame containing data I have scraped from a football website. There are 58 columns(Variables,attributes) for each row. Out of these variables, I wish to plot 3 in a single bar chart.I have 3 important variables 'Name', 'Goals.with.right.foot', 'Goals.with.left.foot'. What I want to build is a bar chart with each 'Name' appearing on the x-axis and 2 independent bars representing the other 2 variables. Sample row entry: {......., RONALDO, 10(left), 5(right),............} I have tried playing around a lot with ggplot2 geom_bar with no success. I have also searched for similar questions however I cannot understand the answers. Is anyone able to explain simply how do I solve this problem? my data frame is called 'Forwards' who are the strikers in a game of football. They have attributes Name, Goals.with.left.foot and Goals.with.right.foot. barplot(counts, main="Goals", xlab="Goals", col=c("darkblue","red"), legend = rownames(counts))
You could try it this way: I simulated a frame as a stand in for yours, just replace it with a frame containing the columns you're interested in: df <- data.frame(names = letters[1:5], r.foot = runif(5,1,10), l.foot = runif(5,1,10)) # transform your df to long format library(reshape2) plotDf <- melt(df, variable.name = 'footing', value.name = 'goals') # plot it library(ggplot2) ggplot(plotDf, aes(x = names, y = goals, group = footing, fill = footing)) + geom_col(position = position_dodge()) #does the same as geom_bar, but uses stat_identity instead of stat_count Results in this plot: your plot This works, because ggplot expects one variable containing the values needed for the y-axis and one or more variable containing the grouping factor(s). with the melt-function, your data.frame is merged into the so called 'long format' which is exactly the needed orientation of data.
Plot multiple traces in R
I started learning R for data analysis and, most importantly, for data visualisation. Since I am still in the switching process, I am trying to reproduce the activities I was doing with Graphpad Prism or Origin Pro in R. In most of the cases everything was smooth, but I could not find a smart solution for plotting multiple y columns in a single graph. What I usually get from the softwares I use for data visualisations look like this: Each single black trace is a measurement, and I would like to obtain the same plot in R. In Prism or Origin, this will take a single copy-paste in a XY graph. I exported the matrix of data (one X, which indicates the time, and multiple Y values, which are the traces you see in the image). I imported my data in R with the following commands: library(ggplot2) #loaded ggplot2 Data <- read.csv("Directory/File.txt", header=F, sep="") #imported data DF <- data.frame(Data) #transformed data into data frame If I plot my data now, I obtain a series of columns, where the first one (called V1) is the X axis and all the others (V2 to V140) are the traces I want to put on the same graph. To plot the data, I tried different solutions: ggplot(data=DF, aes(x=DF$V1, y=DF[V2:V140]))+geom_line()+theme_bw() #did not work plot(DF, xy.coords(x=DF$V1, y=DF$V2:V140)) #gives me an error plot(DF, xy.coords(x=V1, y=c(V2:V10))) #gives me an error I tried the matplot, without success, following the EZH guide: The code I used is the following: matplot(x=DF$V1, type="l", lty = 2:100) The only solution I found would be to individually plot a command for each single column, but it is a crazy solution. The number of columns varies among my data, and manually enter commands for 140 columns is insane. What would you suggest? Thank you in advance. Here there are also some data attached.Data: single X, multiple Y
I tried using the matplot(). I used a very sample data which has no trend at all. so th eoutput from my code shall look terrible, but my main focus is on the code. Since you have already tried matplot() ,just recheck with below solution if you had done it right! set.seed(100) df = matrix(sample(1:685765,50000,replace = T),ncol = 100) colnames(df)=c("x",paste0("y", 1:99)) dt=as.data.frame(df) matplot(dt[["x"]], y = dt[,c(paste0("y",1:99))], type = "l")
If you want to plot in base R, you have to make a plot and add lines one at a time, however that isn't hard to do. we start by making some sample data. Since the data in the link seemed to all be on the same scale, I will assume your data frame only has y values and the x value is stored separately. plotData <- as.data.frame(matrix(sort(rnorm(500)),ncol = 5)) xval <- sort(sample(200, 100)) Now we can initialize a plot with the first column. plot(xval, plotData[[1]], type = "l", ylim = c(min(plotData), max(plotData))) type = "l" makes a line plot instead of a scatter plot ylim = c(min(plotData), max(plotData)) makes sure the y-axis will fit all the data. Now we can add the rest of the values. apply(plotData[-1], 2, lines, x = xval) plotData[-1] removes the column we already plotted, apply function with 2 as the second parameter means we want to execute a function on every column, lines defines the function we are applying to the columns. lines adds a new line to the current plot. x = xval passes an extra parameter (x) to the lines function.
if you wat to plot the data using ggplot2, the data should be transformed to long format; library(ggplot2) library(reshape2) dat <- read.delim('AP.txt', header = F) # plotting only first 9 traces # my rstudio will crach if I plot the full data; df <- melt(dat[1:10], id.vars = 'V1') ggplot(df, aes(x = V1, y = value, color = variable)) + geom_line() # if you want all traces to be in same colour, you can use ggplot(df, aes(x = V1, y = value, group = variable)) + geom_line()
Graphing a Multi-Series Bar/Dot Plot with R
So I'm having trouble creating a dot plot/bar graph of this data set I have. My data set looks like this. I want an output that looks like this. However, geom_bar() through ggplot will only give me counts, and won't take the individual decimal values from the table. I've tried using Plotly as well, but it doesn't seem to scale well to plots with multiple players. I've already set up a larger data frame with 200+ variables. I'm trying to make something that can search for specific players in that data frame, and then create a plot from it. Consequently, I'm ideally looking for something that can easily handle 5-10 different series. Any help would be greatly appreciated. Thanks!
This is pretty straightforward, the key is to get your data from its current wide format into the long format that is more useful for plotting in R. And use geom_point rather than geom_bar. First, some reproducible example data (that you should use again in your question if you post another question here, makes it much easier for others to help you): library(ggplot2) library(reshape2) dataset <- data.frame( PlayerName = letters[1:6], IsolationPossG = runif(6), HandoffPossG = runif(6), OffScreenPossG = runif(6) ) This is your current data, in the wide format: dataset PlayerName IsolationPossG HandoffPossG OffScreenPossG 1 a 0.78184751 0.939183520 0.74461784 2 b 0.06557433 0.745699149 0.96540299 3 c 0.21105745 0.753534811 0.02977973 4 d 0.41271918 0.555475622 0.18317886 5 e 0.38153149 0.246292074 0.74862310 6 f 0.89946318 0.008412111 0.53195933 Now we convert to the long format: molten <- melt( dataset, id.vars = "PlayerName", measure.vars = c("IsolationPossG", "HandoffPossG", "OffScreenPossG") ) Here is the long format, much more useful for plotting in R: head(molten) PlayerName variable value 1 a IsolationPossG 0.78184751 2 b IsolationPossG 0.06557433 3 c IsolationPossG 0.21105745 4 d IsolationPossG 0.41271918 5 e IsolationPossG 0.38153149 6 f IsolationPossG 0.89946318 Here's how to plot it: ggplot(molten, aes(x = variable, y = value, colour = PlayerName)) + geom_point(size = 4) + theme_bw() + theme(legend.position="bottom",legend.direction="horizontal") Which gives: h/t how to have multple labels in ggplot2 for bubble plot If you want the shape of the data point to vary by name, as your example image shows (but it seems rather excessive to have the player name variable on two of the plot's aesthetics): ggplot(molten, aes(x = variable, y = value, shape = PlayerName, colour = PlayerName)) + geom_point(size = 4) + theme_bw() + theme(legend.position="bottom",legend.direction="horizontal")
Vertical data groups from table in R
I have data in table in vertical groups of 7 columns which I need to plot against each other to find relationships. How do I group them vertically in groups of 7 and what plots would be helpful for nice graphics? I looked at "car" which seems appropriate. A_0,A_1,A_2,A_3,A_4,A_5,A_6,B_0,B_1,B_2,B_3,B_4,B_5,B_6,C_0,C_1,C_2,C_3,C_4,C_5,C_6,D_0,D_1,D_2,D_3,D_4,D_5,D_6,D_0,D_1,D_2,D_3,D_4,D_5,D_6,E_0,E_1,E_2,E_3,E_4,E_5,E_6 0,2,0,0,1,1,1,0,1,0,0,1,1,1,0,0.000003,0,0,0.000004,0.000004,0.000004,0,1,0,0,1,1,1,0,2,0,0,1,1,1,0,0.000003,0,0,0.000002,0.000002,0.000002 2,1,0,0,0,0,4,2,1,0,0,0,0,4,0.000007,0.000003,0,0,0,0,0.000012,1,1,0,0,0,0,1,2,1,0,0,0,0,4,0.000003,0.000002,0,0,0,0,0.000006 1,0,0,0,0,4,1,1,0,0,0,0,4,1,0.000003,0,0,0,0,0.000012,0.000003,1,0,0,0,0,1,1,1,0,0,0,0,4,1,0.000002,0,0,0,0,0.000006,0.000002 1,0,0,1,0,0,1,1,0,0,1,0,0,1,0.000003,0,0,0.000003,0,0,0.000001,1,0,0,1,0,0,1,1,0,0,1,0,0,1,0.000002,0,0,0.000001,0,0,0 0,1,0,0,1,2,3,0,1,0,0,1,2,3,0,0.000003,0,0,0.000001,0.000002,0.000003,0,1,0,0,1,1,1,0,1,0,0,1,2,3,0,0.000001,0,0,0,0.000001,0.000002 1,0,0,1,2,3,0,1,0,0,1,2,3,0,0.000003,0,0,0.000001,0.000002,0.000003,0,1,0,0,1,1,1,0,1,0,0,1,2,3,0,0.000001,0,0,0,0.000001,0.000002,0 1,0,0,0,2,0,2,1,0,0,0,2,0,2,0.000001,0,0,0,0.000002,0,0.000002,1,0,0,0,1,0,1,1,0,0,0,2,0,2,0,0,0,0,0.000001,0,0.000001 0,2,0,0,0,1,1,0,2,0,0,0,1,1,0,0.000002,0,0,0,0.000001,0.000001,0,1,0,0,0,1,1,0,2,0,0,0,1,1,0,0.000001,0,0,0,0.000001,0 2,0,0,0,1,1,2,2,0,0,0,1,1,2,0.000002,0,0,0,0.000001,0.000001,0.000002,1,0,0,0,1,1,1,2,0,0,0,1,1,2,0.000001,0,0,0,0.000001,0,0.000001 0,1,1,2,2,0,3,0,1,1,2,2,0,2,0,0.000001,0.000001,0.000002,0.000002,0,0.000003,0,1,1,1,1,0,1,0,1,1,2,2,0,3,0,0.000001,0,0.000001,0.000001,0,0.000001 1,1,2,2,0,3,1,1,1,2,2,0,2,1,0.000001,0.000001,0.000002,0.000002,0,0.000003,0.000001,1,1,1,1,0,1,1,1,1,2,2,0,3,1,0.000001,0,0.000001,0.000001,0,0.000001,0.000001 0,1,1,1,2,2,0,0,1,1,1,1,2,0,0,0.000001,0.000001,0.000001,0.000002,0.000002,0,0,1,1,1,1,1,0,0,1,1,1,2,2,0,0,0.000001,0.000001,0,0.000001,0.000001,0 0,1,1,0,0,0,12,0,1,1,0,0,0,6,0,0.000001,0.000001,0,0,0,0.000007,0,1,1,0,0,0,1,0,1,1,0,0,0,12,0,0,0,0,0,0,0.000006 1,1,0,0,0,12,34,1,1,0,0,0,6,15,0.000001,0.000001,0,0,0,0.000007,0.000017,1,1,0,0,0,1,1,1,1,0,0,0,12,34,0,0,0,0,0,0.000006,0.000015 1,0,0,0,12,34,1,1,0,0,0,6,15,0,0.000001,0,0,0,0.000007,0.000017,0.000001,1,0,0,0,1,1,1,1,0,0,0,12,34,1,0,0,0,0,0.000006,0.000015,0 1,0,13,4,11,9,10,0,0,1,2,1,5,7,0.000001,0,0.000002,0.000002,0.000002,0.000005,0.000008,1,0,1,1,1,1,1,1,0,13,4,11,9,10,0,0,0.000006,0.000002,0.000005,0.000004,0.000005 0,13,4,11,9,10,18,0,1,2,1,5,7,4,0,0.000002,0.000002,0.000002,0.000005,0.000008,0.000006,0,1,1,1,1,1,1,0,13,4,11,9,10,18,0,0.000006,0.000002,0.000005,0.000004,0.000005,0.000008 13,4,11,9,10,18,39,1,2,1,5,7,4,9,0.000002,0.000002,0.000002,0.000005,0.000008,0.000006,0.000011,1,1,1,1,1,1,1,13,4,11,9,10,18,39,0.000006,0.000002,0.000005,0.000004,0.000005,0.000008,0.000017 4,11,9,10,18,39,8,2,1,5,7,4,9,4,0.000002,0.000002,0.000005,0.000008,0.000006,0.000011,0.000005,1,1,1,1,1,1,1,4,11,9,10,18,39,8,0.000002,0.000005,0.000004,0.000005,0.000008,0.000017,0.000004 11,9,10,18,39,8,16,1,5,7,4,9,4,5,0.000002,0.000005,0.000008,0.000006,0.000011,0.000005,0.000006,1,1,1,1,1,1,1,11,9,10,18,39,8,16,0.000005,0.000004,0.000005,0.000008,0.000017,0.000004,0.000007 9,10,18,39,8,16,0,5,7,4,9,4,5,0,0.000005,0.000008,0.000006,0.000011,0.000005,0.000006,0,1,1,1,1,1,1,0,9,10,18,39,8,16,0,0.000004,0.000005,0.000008,0.000017,0.000004,0.000007,0 1,0,1,6,0,0,2,1,0,1,3,0,0,2,0.000001,0,0.000001,0.000003,0,0,0.000002,1,0,1,1,0,0,1,1,0,1,6,0,0,2,0,0,0,0.000003,0,0,0.000001 0,1,6,0,0,2,2,0,1,3,0,0,2,2,0,0.000001,0.000003,0,0,0.000002,0.000002,0,1,1,0,0,1,1,0,1,6,0,0,2,2,0,0,0.000003,0,0,0.000001,0.000001 0,2,2,0,0,8,4,0,2,2,0,0,8,4,0,0.000002,0.000002,0,0,0.000011,0.000006,0,1,1,0,0,1,1,0,2,2,0,0,17,19,0,0.000001,0.000001,0,0,0.000008,0.000009 2,2,0,0,8,4,1,2,2,0,0,8,4,1,0.000002,0.000002,0,0,0.000011,0.000006,0.000002,1,1,0,0,1,1,1,2,2,0,0,17,19,3,0.000001,0.000001,0,0,0.000008,0.000009,0.000001 2,0,0,8,4,1,1,2,0,0,8,4,1,2,0.000002,0,0,0.000011,0.000006,0.000002,0.000003,1,0,0,1,1,1,0.5,2,0,0,17,19,3,5,0.000001,0,0,0.000008,0.000009,0.000001,0.000002
Since you haven't put data or specified what sort of chart, I shall put untested code assuming a bubble plot - library(ggplot2) library(reshape2 datamelted <- melt(data, id.vars = 'ABcol') ggplot(datamelted, aes(x = ABcol, y = variable, size = value) + geom_point()