This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to create grouped barplot with R
Species Dbh Height
1 DF 383.7143 254.3036
2 ES 403.3333 280.0000
3 F 372.0000 270.0000
4 FG 381.5000 275.0000
5 GF 351.5838 242.6522
6 HW 209.0000 198.0000
7 LP 232.8571 218.3333
8 PP 568.5000 330.0000
9 SF 136.4286 154.1000
10 WC 375.0757 234.8777
11 WL 340.0588 252.5714
12 WP 319.7273 251.3939
I want to turn the data above into a bar graph like this one. Species as bins and dbh and height as bars for each bin.
I used the code:
aggregate(ufc[,4:5],ufc[3],mean,na.rm=TRUE)
to get the above data set
I can only get one variable at a time using this code:
barplot(ufc.means$Height, col=rainbow(20),
names.arg=(ufc.means$Species), las=2,main="Height")
First, do not double post! If necessary edit your previous question, rather than posting a new question.
Second, the error message that you mentioned in your earlier post is pretty self-explanatory. Here's the error message:
Error in barplot.default(ufc.means, col = rainbow(20), names.arg = (ufc.means$Species), : 'height' must be a vector or a matrix
Read that last part carefully: 'height' must be a vector or a matrix, but you are trying to use a data.frame. So, the solution is easy: convert your data.frame to a matrix before using barplot.
Assuming your data.frame is named "mydf":
mymat <- t(mydf[-1])
colnames(mymat) <- mydf[, 1]
barplot(mymat, beside = TRUE)
Result:
Related
I have a dataframe called Insectsprays that has two columns, count and spray. When I try to use split to create boxplots for each value of spray, I get the error shown below.
Can anyone explain the error for me? It's no doubt clear I'm new to R.
#
class(InsectSprays)
[1] "data.frame"
#
head(InsectSprays)
count spray
1 10 A
2 7 A
3 20 A
4 14 A
5 14 A
6 12 A
#
boxplot(split(x=InsectSprays,f=InsectSprays$spray))
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) :
'x' must be atomic
boxplot expects a basic ('atomic') object like a series of numbers 1:10 or a list of basic atomic objects list(1:10,2:11). Your split produces a list of data.frames which boxplot doesn't know how to handle. Luckily, boxplot can also take a formula if you want to get results per group, like:
boxplot(count ~ spray, data=InsectSprays)
If you were working with a different function that didn't have this possibility, you would need to loop over the split list. Possibly something like:
## divide the plot window into 3 columns/2 rows
par(mfrow=c(2,3))
## loop over each object and `boxplot` the `count` column
lapply(split(InsectSprays, InsectSprays$spray), \(x) boxplot(x$count) )
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
I load the supportdata variable as follows.
supportdata=aggregate(scoredata$Support, list(Topic = scoredata$Topic), sum)
slices <- supportdata[2]
lbls <- supportdata[1]
typeof(slices)
3D Exploded Pie Chart Below
pie3D(slices,labels=lbls,explode=0.1,main="Year wise scores for topic 1")
and I get the below error:
Error in pie3D(slices, labels = lbls, explode = 0.1, main = "Year wise
scores for topic 1") :pie3D: x values must be positive numbers
supportdata variable contains the following information and is generated using aggregate function which sums up the scores in the second column.
# supportdata
#
# Topic x
#
# 1 c 14
# 2 c# 80
# 3 c++ 15
# 4 css 4
# 5 html 3
# 6 .net 3
# 7 php 0
# 8 sql 0
How do I get rid of this error? I tried searching but couldn't find a solution to this problem..I tried casting into as.numeric, as.integer but it says the list cannot be coerced into double or integer type. :(
Your problem is indexing with [ rather than [[, which returns a list of numbers rather than a numeric vector.
library("plotrix")
pie3D(supportdata[[2]],labels=supportdata[[1]],
explode=0.1,main="Year wise scores for topic 1")
works fine, as does
with(supportdata,pie3D(x,labels=Topic,
explode=0.1,main="Year wise scores for topic 1"))
The below solution works too apart from one provided by Ben.
slices <- t(supportdata[2])
lbls <- t(supportdata[1])
pie3D(slices,labels=lbls,explode=0.1,main="Pie Diagram for Support")
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, ...)
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)