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 am a novice R user, hence the question. I refer to the solution on creating stacked barplots from R programming: creating a stacked bar graph, with variable colors for each stacked bar.
My issue is slightly different. I have 4 column data. The last column is the summed total of the first 3 column. I want to plot bar charts with the following information 1) the summed total value (ie 4th column), 2) each bar is split by the relative contributions of each of the three column.
I was hoping someone could help.
Regards,
Bernard
If I understood it rightly, this may do the trick
the following code works well for the example df dataframe
df <- a b c sum
1 9 8 18
3 6 2 11
1 5 4 10
23 4 5 32
5 12 3 20
2 24 1 27
1 2 4 7
As you don't want to plot a counter of variables, but the actual value in your dataframe, you need to use the goem_bar(stat="identity") method on ggplot2. Some data manipulation is necessary too. And you don't need a sum column, ggplot does the sum for you.
df <- df[,-ncol(df)] #drop the last column (assumed to be the sum one)
df$event <- seq.int(nrow(df)) #create a column to indicate which values happaned on the same column for each variable
df <- melt(df, id='event') #reshape dataframe to make it readable to gpglot
px = ggplot(df, aes(x = event, y = value, fill = variable)) + geom_bar(stat = "identity")
print (px)
this code generates the plot bellow
How can I plot 7 different graphs on one pdf page on R?
I currently use matplot, which doesn't seem to have this option. I need to plot columns of data against columns of data.
I initially tried to do this with the lattice library, but I can't seem to figure out how to plot the columns of data. It seems to want a function.
To create a pdf of plots, you can do something like this. To initialize a pdf document use the pdf function with a file name first. dev.off() at the end will close the graphics device and complete the pdf. Afterwards, you should see a new document in the working directory (in this example - 'plots.pdf').
d <- data.frame(matrix(sample(c(1:700), 2000, TRUE), 10, 20))
pdf('plots')
par(mfrow = c(3, 3)) ## set the layout to be 3 by 3
sapply(1:9, function(i) plot(d[,i]))
dev.off()
Which produces this pdf
If you want to do this with base graphics, I strongly recommend using the layout() function. It takes a matrix which defines how to split up the window. It will make a row for every row in your matrix and a column for every column. It draws the plots in order of the number of the cells. So if you pass the matrix
#layout(matrix(c(1:7,7), ncol=2, byrow=T))
# [,1] [,2]
#[1,] 1 2
#[2,] 3 4
#[3,] 5 6
#[4,] 7 7
the first plot will go in the upper left, the second in the upper right, etc, until the 7th plot goes all the way at the bottom. You could just have it take up only the bottom left if you like by specifying a different number in the bottom right.
To reset the layout back to "normal" just call
layout(1)
you could then create a for loop to make each plot.
If you want one plot to do all pairwise comparisons, the pairs() plotting function might be what you want
dd<-matrix(runif(5*5), ncol=5)
pairs(dd)
or the lattice equivalent is splom()
I have 2 data frames, mydf1 and mydf2
> mydf1
id a b c
1 1 2 10 2
2 2 3 11 4
3 3 5 12 6
4 4 7 13 8
5 5 8 14 10
> mydf2
id a b c
1 1 4 20 4
2 2 6 22 8
3 3 10 24 12
4 4 14 26 16
5 5 16 28 20
I would like to plot variables a,b & c against id (sample graphs is given below). I want similar graphs for variables b and c too and I want to do it in a loop and then export it to a local folder. So, I am using the following code
for (i in 2:4) {
jpeg(paste("C:/Data/myplot",i,".jpg"))
ymin<-min(mydf1[,i],mydf2[,i])
ymax<-max(mydf1[,i],mydf2[,i])
plot(mydf1[,1],mydf1[,i],ylim=c(ymin,ymax),xlab="id",ylab=colnames(mydf)[i])
points(mydf2[,1],mydf2[,i],pch=2)
legend("topright",c("mydf1","mydf2"),pch=c(1,2))
dev.off()
}
My problem is that I would like to get all three different graphs, (id vs a (mydf1 and mydf2) , id vs b(mydf1 and mydf2), id vs c(mydf1 and mydf2) in one figure.(something like 2 along the first row of the figure and the third one in the second row with legend) I tried the following
jpeg("C:/Data/myplot.jpg")
par(mfrow=c(2,2))
for (i in 2:4) {
ymin<-min(mydf1[,i],mydf2[,i])
ymax<-max(mydf1[,i],mydf2[,i])
plot(mydf1[,1],mydf1[,i],ylim=c(ymin,ymax),xlab="id",ylab=colnames(mydf)[i])
points(mydf2[,1],mydf2[,i],pch=2)
legend("topright",c("mydf1","mydf2"),pch=c(1,2))
dev.off()
}
But it didn't work. Any suggestion to do this?
p.s: This is the simplified version of my task. Actually I have hundreds of columns, that's why I am using a loop operation
Sample plot id vs a (mydf1 and mydf2) plotted on the same graph
It is unclear what you are trying to do. Do you want 2 plots, one for mydf1 and one for mydf2 or all on one figure? If two panels, you should change to mfrow=c(2,1) instead of c(2,2) which is currently making 4 panels?
If you want them all on a single plot, then remove the par(mfrow... line.
Then within the plots, you are plotting the first series from mydf1 and the other two series from mydf2. Is that actually what you want?
Using base graphics, you should move your plot line outside the loop so it is done once, change the loop to start at 3, and then keep the points statements inside the loop. Alternatively, you could put an if statement inside the loop to see if it is the first time.
You also have a typo in the plot statement with mydf (no number).
And move your dev.off() outside the loop so it only closes the figure once.
Here is some code that generates a single-panel plot, and you should be able to modify it to work for your desired output...
jpeg("myplot.jpg")
for (i in 2:4) {
ymin<-min(mydf1[,i],mydf2[,i])
ymax<-max(mydf1[,i],mydf2[,i])
if (i==2){
plot(mydf1[,1],mydf1[,i],ylim=c(ymin,ymax),xlab="id",ylab=colnames(mydf1)[i])
legend("topright",c("mydf1","mydf2"),pch=c(1,2))
}
else{
points(mydf2[,1],mydf2[,i],pch=2)
}
}
dev.off()
EDIT: After your clarified question, I think the only problem is that dev.off() should be outside the loop. (I recommend PNG or PDF instead of JPEG for any plot worth presenting....)
png("myplot.png")
par(mfrow=c(2,2))
for (i in 2:4) {
ymin<-min(mydf1[,i],mydf2[,i])
ymax<-max(mydf1[,i],mydf2[,i])
plot(mydf1[,1],mydf1[,i],ylim=c(ymin,ymax),xlab="id",ylab=colnames(mydf1)[i])
legend("topright",c("mydf1","mydf2"),pch=c(1,2))
points(mydf2[,1],mydf2[,i],pch=2)
}
dev.off()
I'd do something like
mydf1$g <- 1
mydf2$g <- 2
d3 <- rbind(mydf1, mydf2)
library(reshape2)
d3 <- melt(d3, id.vars = c('id', 'g'))
library(ggplot2)
ggplot(d3, aes(x=id, y=value)) +
geom_point(aes(colour = as.factor(g), shape = variable))
or using facets
ggplot(d3, aes(x=id, y=value)) +
geom_point(aes(colour = as.factor(g))) +
facet_wrap(~variable)
to finally export it
ggsave(file = paste0(tempdir(), 'myplot.png'),
last_plot()
)
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, ...)