Could you please help me to solve this problem:
I have a database like below:
Animal Milk Age
1 11.96703591 1
1 13.41236333 2
1 14.85769075 3
1 16.30301817 4
2 17.74834559 1
2 19.08465881 2
2 20.42097204 3
2 14.66094662 4
2 14.70197368 5
3 14.74300075 1
3 14.78402781 2
3 14.82505488 3
3 14.86608194 4
3 14.90710901 5
I want to make a plot between milk versus age, so I use function plot(Milk~Age, data=mydata)
My question is how can I make the same plot (Milk~Age) for each individual, by using only one function. Since I have about 200 animals and if I have to run 200 times to produce 200 curves.
Thanks
Phuong
One approach would be to use library ggplot2 and then make individual facets for each animal. As you have many animals you can change ncol= or nrow= in facet_wrap() to get better view.
library(ggplot2)
ggplot(df,aes(x=Age,y=Milk))+geom_point()+facet_wrap(~Animal)
The following code should create as many plot as you have unique Animal values, and store them in different pdf files in the working directory :
invisible(by(df, df$Animal, function(tmpdf) {
pdf(paste0("plot",tmpdf$Animal[1],".pdf"))
plot(Milk~Age, data=tmpdf, main=tmpdf$Animal[1])
dev.off()
}))
I would say to use ggplot from the ggplot2 package
ggplot(df,aes(x=Age,y=Milk, color=Animal))+geom_point()
edit1: actually this would lose clarity with 200 animals. Did you want all this data point in one graph or spread out across 200 graphs? If the latter then I agree with Didzis
Related
I am Beginner in R and I want to create network plot in one column based on another column.
Here an example of what my data frame looks like:
## project-ID ## ## Area-ID ##
1 2
1 3
1 5
2 4
2 2
2 3
so the network plot will show the relation between AreaID ,I didn't found any idea that will help me
I hope someone can help. Thank you!
For future posts, please review how to ask questions here on SO. Generally you are more likely to receive help if you show (1) a decent amount of research effort, and (2) a code attempt.
That aside, the following should get you started.
We can convert the data.frame to an igraph object, and plot the graph.
# Sample data
df <- read.table(text =
"project-ID Area-ID
1 2
1 3
1 5
2 4
2 2
2 3", header = T)
# Convert data.frame to igraph and plot
library(igraph);
ig <- graph_from_data_frame(df);
plot(ig);
Many resources involving plotting and analysing networks/graphs using igraph can be found online, e.g. here, here, here, ...
I have a continuous stream of data in two columns that I am trying to plot. The data contains different trajectories however, and I want gnuplot to plot theses with lines but not connect the different trajectories. How would I signal gnuplot to recognize these different trajectories and not connect them?
Eg:
1 1
2 4
3 9
new traj
1 1
2 .5
3 .333
Sorry if this has been posted before, I searched for about an hour and gave up. Thanks in advance.
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
Consider the following frequency data:
> table(income)
income
3 5 6 7 8 5000
2 7 2 2 2 1
When I type >hist(income) I get the following histogram
So as you can see, the fact that most income values are concentrated around 5 and there is one value quite distant from the others makes the histogram not look very good. MS Excel can consider the 5000 value as of another category, so the data would like this instead:
> table(income)
income
3 5 6 7 8 more
2 7 2 2 2 1
So plotting this as a histogram would look much better, so you can see the frequency within a shorter range:
Is there anyway to do this either with the hist() function or others functions from lattice or ggplot2? I do however, don't want to overwrite the values that exceed a certain threshold, so as I do lose any information.
Thanks a lot!
Data generation:
income <- c(rep(3,2), rep(5,7), rep(6,2), rep(7,2), rep(8,2), 5000)
Function for preparing data for plotting:
nice.data <- function(x, threshold=10){
x[x>threshold] <- "More"
x
}
Plotting:
library(ggplot2)
ggplot() + geom_histogram(aes(x=nice.data(income))) + xlab("Income")
Result:
I have a dataframe dbwith 2 categorical variables: varA has 4 levels (0,1,2,3), varB has 2 levels (yes,no). varB has no values for the level 0 of varA:
id varA varB
1 2 yes
2 3 no
3 3 no
4 1 yes
5 0 NA
6 1 no
7 2 no
8 3 yes
9 3 yes
10 2 no
I created a contingency table using CrossTable from the descr package and then a mosaic plot with the plot function:
table <- CrossTable(db$varA,db$varB, missing.include=FALSE)
plot(table,xlab="varA",ylab="varB")
I obtained this plot:
I would like to eliminate the level 0 from the plot. I also would like to add 2 y-axis, one on the left of the plot with a scale from 0 to 1 and one on the right with a scale from 1 to 0.
Could you help me?
Well, that was annoying. There is no support for subsetting such a "CrossTable" object. If it were a well-behaved table-like object you would been able to just pass table[ , -1] to the plot function. instead you need to do the subetting before the data that is passed to CrossTable:
table <- with( na.omit(db), CrossTable( varA, varB, missing.include=TRUE))
plot(table, xlab="varA", ylab="varB")
BTW using the name table for a data-object is quite confusing to regular R users since the table function is one of our basic tools.
Personally I would avoid avoid using that CrossTable function since its output is so weird and not available for management with typical R functions. Yeah, I know it produces a SAS-like output, but R users grow to love the compact output of the table function and the many matrix operations that are available for working with table-objects. You may need to get your margin percentages by hand with prop.table.