I have data as follows in .csv format as I am new to ggplot2 graphs I am not able to do this
T L
141.5453333 1
148.7116667 1
154.7373333 1
228.2396667 1
148.4423333 1
131.3893333 1
139.2673333 1
140.5556667 2
143.719 2
214.3326667 2
134.4513333 3
169.309 8
161.1313333 4
I tried to plot a line graph using following graph
data<-read.csv("sample.csv",head=TRUE,sep=",")
ggplot(data,aes(T,L))+geom_line()]
but I got following image it is not I want
I want following image as follows
Can anybody help me?
You want to use a variable for the x-axis that has lots of duplicated values and expect the software to guess that the order you want those points plotted is given by the order they appear in the data set. This also means the values of the variable for the x-axis no longer correspond to the actual coordinates in the coordinate system you're plotting in, i.e., you want to map a value of "L=1" to different locations on the x-axis depending on where it appears in your data.
This type of fairly non-sensical thing does not work in ggplot2 out of the box. You have to define a separate variable that has a proper mapping to values on the x-axis ("id" in the code below) and then overwrite the labels with the values for "L".
The coe below shows you how to do this, but it seems like a different graphical display would probbaly be better suited for this kind of data.
data <- as.data.frame(matrix(scan(text="
141.5453333 1
148.7116667 1
154.7373333 1
228.2396667 1
148.4423333 1
131.3893333 1
139.2673333 1
140.5556667 2
143.719 2
214.3326667 2
134.4513333 3
169.309 8
161.1313333 4
"), ncol=2, byrow=TRUE))
names(data) <- c("T", "L")
data$id <- 1:nrow(data)
ggplot(data,aes(x=id, y=T))+geom_line() + xlab("L") +
scale_x_continuous(breaks=data$id, labels=data$L)
You have an error in your code, try this:
ggplot(data,aes(x=L, y=T))+geom_line()
Default arguments for aes are:
aes(x, y, ...)
Related
I am trying to use the R barplot function to plot the following array on the same graph:
ID 1 2 3 4 5 6 7 8
HeL 0 2 1 4 2 3 2 4
CaC 2 0 0 2 1 5 7 8
NIH 1 2 5 6 3 5 7 9
I would need to have the barplot of each row having its own y-axis, but the x-axis should be common for all rows. What I have achieved so far, is to read the matrix from the file "rna.tab" and then plot each row separately:
dat <- read.table ("rna.tab", row.names=1, header=TRUE)
barplot (as.matrix (dat[,1]))
barplot (as.matrix (dat[,2]))
barplot (as.matrix (dat[,3]))
but I didn't succeed in plotting them all together.
Thanks in advance-
Arturo
Is this what you are looking for? If it isn't could you please make a manual example of what you want and post the image?
par(mfrow = c(ncol(dat),1), mar = c(2.5,4,1,1))
apply(dat, 2, barplot, beside = TRUE)
par(mfrow = c(1,1))
The first par say you want a grid of plots with as many rows as there are columns of dat and 1 column, and changes the margins of the plot to be appropriate. The apply function makes a barplot for eash column of dat and beside = TRUE puts the columns next to each other. The next par resets the plotting grid to a single graph so next time you need to plot something you aren't just making a bunch of tiny plots.
Thanks Barker for the fix and sorry for taking so long to get back to you, but I was sick for almost one week.
Your code works great, the only thing is that, since I need to plot the rows and not the columns, it should be:
apply(dat, 1, barplot, beside = TRUE)
Sorry for not being clear about this point.
I have just one last question, if you don't mind. Usually my real life matrix is 6000*30. This means that I have to plot 30 rows.
Usually I save the image to disk:
png ("plot.png")
par(mfrow = c(ncol(dat),1), mar = c(2.5,4,1,1))
apply(dat, 1, barplot, beside = TRUE)
dev.off ()
When I do this, I get only the plot of the last 4 rows in the file "plot.png", instead of the plot of all rows. Also, since the x-axis is the same for all plots, would be possible to draw it only at the end?
I am trying to create a line plot in R. For each 'RuleID' in my data frame I want to plot the 'ErrorCount' at each 'ProcessorTimeStamp'
DQ_Counts= data.frame(RuleID=c(1,2,1,2),
ProcessorTimeStamp=as.Date(c('2016-08-04','2016-08-04','2016-08-08','2016-08-08')),
ErrorCount=c(6,8,3,4))
# RuleID ProcessorTimeStamp ErrorCount
# 1 1 2016-08-04 6
# 2 2 2016-08-04 8
# 3 1 2016-08-08 3
# 4 2 2016-08-08 4
This is a plot I found online that I would like the end result to look like all though I am obviously not talking about trees. The code for this plot is here Code for Tree Growth Plot but I don't understand it well enough to make it work for me.
For my plot 'ProcessTimeStamp' would be my x and 'ErrorCount' would by my y. Each line would represent a different 'RuleID'.
One thing to note is that I have 'ErrorCounts' ranging from 0 to over 3 million (this is why I need to report on them to get them fixed!).
Thanks in advance.
This is probably the easiest way to get a basic plot like the one above with your data
lattice::xyplot(ErrorCount~ProcessorTimeStamp, DQ_Counts,
groups=RuleID, auto.key=T, type="l")
Which returns
or you could use ggplot2
library(ggplot2)
ggplot(DQ_Counts, aes(ProcessorTimeStamp, ErrorCount, color=factor(RuleID))) + geom_line()
to get
I've created an interaction plot and realized that one line breaks in the middle and that is because two factors on the x axis has no value for that particular factor, although for the other factors, it has a value. It looks like this (assume the lines shown are actually connected):
_
/ _ /
1 2 3 4 5 6 7
x axis
Basically there is a value for 4, but not for 3 and 5 so that's why it looks as it does. How do I correct this? I have set everything as a factor
I read to use na.rm but that doesn't work and it looks the same.
Thanks
I want to create a mosaic plot in R using mosaic from the vcd library. The table that I am plotting has many many 0's in it (when I plot it, the mosaic plot is incomprehensible), and I would like to create a mosaic plot with just the top 25 highest entries. How do you subset a table in R to accomplish this? Or, how do you change every entry satisfying a certain condition to 0?
As an example:
df <- data.frame(letters=c("a","b","c","c","b","c","a","b"), end=c("x","y","x","y","x","y","y","x"))
t <- table(df)
The table looks like this:
> t
end
letters x y
a 1 1
b 2 1
c 1 2
I would like to substitute each 1 to be a 0. How should I do this?
The replace each 1 with a 0:
t[t==1] <- 0
This question already has answers here:
Creating a histogram using aggregated data
(4 answers)
Closed 9 years ago.
HI Guys I'm trying to plot a frequency graph of a simple 2d file
file:data.csv
terms,count
1,10
5,17
3,28
9,30
I want the first col(terms) to be the x-axis and the col(count) be the height/percentage.
I've tried this:
d<-read.csv(data.csv)
hist(d)
Error in hist.default(d) : 'x' must be numeric
dc<-table(d)
hist(dc) <-- wrong result.
The problem is that hist() needs a vector containing your objects as often as they are present in your data. Your are providing it a frequency table.
See this:
> df <- data.frame(obj = c(1,2,3,4,5), count = c(2,3,5,4,2))
> hist(df)
Error in hist.default(df) : 'x' must be numeric
> hist(rep(df$obj, df$count), breaks=0:5)
[img]
> rep(df$obj, df$count)
[1] 1 1 2 2 2 3 3 3 3 3 4 4 4 4 5 5
rep(a,n) repeats element by element the value of a n-times. Then you have the vector you need and you can hand it to hist().
d<-read.csv(text="terms,count
1,10
5,17
3,28
9,30")
hist(d) # No error ... but not the plot you wanted.
Your lack of quotes around data.csv could be the problem or if the the first line in the file is really file:data.csv, that could be another problem. It does appear, however, that you probably want barchart or barplot, since you have already done the aggregation of the counts.
To illustrate why barchart or barplot could have been use:
require(lattice)
# dividing by total "counts" to get the fractional values
barchart(count/sum(d$count)~factor(terms), data=d)