I would like to create a plot from two values:
results<-data.frame(name=c("A","B"), values=c("0.8639503","0.7870299"))
qplot(name, data=results, geom="bar")
This gives me a plot, where the difference between the two bars is invisible. Can someone help how to create this plot so that the differences are visible?
Using:
ggplot(results, aes(x=name, y=values)) +
geom_bar(stat="identity")
will give you the desired result
Related
I'm trying to convert the decimal points from the colomn titled cover_type_ratio below into a percentage in ggplot2. Here is the df
DF
And here is my code for my bar graph:
plot<-ggplot(coverType_count, aes(x=Cover_Type, y=count)) +
geom_bar(stat="identity") +
scale_y_continuous() +
geom_text(data=coverType_count,aes(label=count,y=count+100),size=4) +
geom_text(data=coverType_count,
aes(label=cover_type_ratio,y=count+200),size=4)+
theme(axis.text.x=element_text(angle=30,hjust=1,size=8))+
ggtitle('Cover Type Distribution')
When plotted, it looks like this:
Screenshot of Plot
I need to change the jumbled numbers to percentages from the aforementioned cover_type_ratio column in df, and have tried using the scales library, but to no avail. Does anyone have any suggestions?
Thank you!
I'm trying to accomplish something that I used to do in Excel, I have several timeseries for the same time interval and would like to plot them as lines (easy enough using ggplot geom_line), but one of them should be plotted as an area plot.
Basically something like this:
Plase note that the series S_1 is plotted as area.
I have already tried adding geom_area() with aes values equal to the value of the area series:
ggplot(df.lines, aes(x=Index, y=Value, colour=Series)) + geom_line() + geom_area(aes(x=df.area$Index, y=df.area$S_1))
How could I acomplish something like this using ggplot2?
Difficult to test with no dataset (can you provide one on the example, you can use dput()), but in geom_area, the selection should be made in the data argument.. like this for instance..
ggplot +
geom_area(data = df.area[df.area$Series == "S_1", ], aes(x=Index, y=Value))
geom_line(data = df.lines, aes(x=Index, y=Value, colour=Series))
I'm trying to plot a scatterplot with errorbars together with a barplot. Both variables have a very different range (scatterplot 0 - 20000+; bar chart 0-1). So I want to create a plot with both graphs in it, with two different y-axes. This is what I've got so far:
ggplot(data, aes(x=seaname, y=chao2)) + geom_bar(stat="identity",
aes(x=seaname, y=compl, colour=major_sea)) + geom_point(aes(colour=major_sea))
+ geom_errorbar(aes(ymin=chao2_lower, ymax=chao2_upper,
colour=major_sea)) + theme(axis.text.x=element_text(angle=90,
vjust=0.5, size=9))
Just to clarify: I don't want two graphs represented below each other. I would like them in the same, single graph.
Anyone who has experience with this kind of problem?
Cheers!!
I would like to use ggplot and faceting to construct a series of density plots grouped by a factor. Additionally, I would like to a layer another density plot on each of the facets that is not subject to the constraints imposed by the facet.
For example, the faceted plot would look like this:
require(ggplot2)
ggplot(diamonds, aes(price)) + facet_grid(.~clarity) + geom_density()
and then I would like to have the following single density plot layered on top of each of the facets:
ggplot(diamonds, aes(price)) + geom_density()
Furthermore, is ggplot with faceting the best way to do this, or is there a preferred method?
One way to achieve this would be to make new data frame diamonds2 that contains just column price and then two geom_density() calls - one which will use original diamonds and second that uses diamonds2. As in diamonds2 there will be no column clarity all values will be used in all facets.
diamonds2<-diamonds["price"]
ggplot(diamonds, aes(price)) + geom_density()+facet_grid(.~clarity) +
geom_density(data=diamonds2,aes(price),colour="blue")
UPDATE - as suggested by #BrianDiggs the same result can be achieved without making new data frame but transforming it inside the geom_density().
ggplot(diamonds, aes(price)) + geom_density()+facet_grid(.~clarity) +
geom_density(data=transform(diamonds, clarity=NULL),aes(price),colour="blue")
Another approach would be to plot data without faceting. Add two calls to geom_density() - in one add aes(color=clarity) to have density lines in different colors for each level of clarity and leave empty second geom_density() - that will add overall black density line.
ggplot(diamonds,aes(price))+geom_density(aes(color=clarity))+geom_density()
I'm interested in producing a histogram with position='dodge' and fill=some factor (i.e. side-by-side bars for different subgroups within each bar/group), but ggplot2 gives me something like the first plot here, which has a rightmost bar that's too wide and reserves no space for the empty group, which I would like.
Here's a simple case:
df = data.frame(a=c('o','x','o','o'), b=c('a','b','a','b'))
qplot(a, data=df, fill=b, position='dodge')
From ggplot geom_bar - bars too wide I got this idea, and while it technically produces a bar of the same width, but preserves no space for the empty group:
ggplot(df, aes(x=a, fill=a))+
geom_bar(aes(y=..count../sum(..count..))) +
facet_grid(~b,scales="free",space="free")
How do I achieve what I want? Thanks in advance.
The default options in ggplot produces what I think you describe. The scales="free" and space="free" options does the opposite of what you want, so simply remove these from the code. Also, the default stat for geom_bar is to aggregate by counting, so you don't have to specify your stat explicitly.
ggplot(df, aes(x=a, fill=a)) + geom_bar() + facet_grid(~b)