ggplot geom_bar shows wrong scale - r

I'm stuck. I am trying to make a barplot using ggplot:
ggplot(data_ocena, aes(x=grupa, y=ocena, fill=ocena)) +
geom_bar(stat="identity") +
xlab("częstość występowania poszczególnych ocen na kolokwium")
results in:
how to deal with that? variable ocena is factor, group is factor also.

Related

ggplot line plot by group and sub-group without a facet

I am wondering if I am able to graph separate lines for 2 variables without using the grid function. I would prefer the 4 lines on one graph than 2 lines in 2 grids. Its ok if I can't but thought I would ask.
My data is as follows:
nd<-data.frame(Machine = c(2,2,3,3,2,2,3,3),
Source = c("tube", "machine","tube", "machine","tube", "machine","tube", "machine"),
Time=c(0,0,0,0,2,2,2,2),
Count=c(224000, 107000, 850000, 940000, 610000,116000, 1160000, 1100000))
and this code gives me what I want with a facet...
ggplot(data=nd, aes(x=Time, y=Count, group=Machine, color=Machine)) +
geom_line(aes(group=Machine))+ geom_point()+facet_grid(~Source)
Is there an alternative to this?
P.S. even though Machine is a factor variable why is my legend showing it as continuous?
One quick way is to use the interaction function, which paste your two variables with a "."
ggplot(data=nd, aes(x=Time, y=Count, color=interaction(Machine,Source))) +
geom_line() + geom_point() +
scale_color_manual("groups",
values=c("#61d4b3","#fdd365","#fb8d62","#fd2eb3"))

R continuous vs categorical percentage share with geom_line

I'd like to create a ggplot geom_line graph with continuous data on the x-axis and the percentage share of a categorical variable.
E.g. for mtcars I would like to have hp on the x-axis and the percentage of the cars that have 6 cylinders on the y-axis.
ggplot2(aes(x=hp,y=cyl), data=mtcars) +
geom_line()
I think it needs to be defined in geom_line by fun.y or something similar.
Compute the frequencies beforehand, using reshape for instance :
library(reshape)
M <- melt(mtcars,id.vars="hp",measure.vars="cyl")
C <- cast(M,hp~ variable)
C$f <- C$cyl/sum(C$cyl)
ggplot(C,aes(x=hp,y=f)) +
geom_line()
Note that in that case, a line plot doesn't seem to make much sense, data points are too far appart. You could use a bar plot instead :
ggplot(C,aes(x=hp,y=f)) +
geom_bar(stat="identity")

grouping bars in bar-plot without using facets

I want to group the bars in a stacked barplot according to the values in another factor-variable. However, I want to do this without using facets.
my data in long format
I want to group the stacked bars according the afk variable. The normal stacked bar plot can be made with:
ggplot(nl.melt, aes(x=naam, y=perc, fill=stemmen)) +
geom_bar(stat="identity", width=.7) +
scale_x_discrete(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
coord_flip() +
theme_bw()
which gives an alfabetically ordered barplot:
I tried to group them by using x=reorder(naam,afk) in the aes. But that didn't work. Also using group=afk does not have the desired effect.
Any ideas how to do this?
reorder should work but the problem is you're trying to re-order by a factor. You need to be explicit on how you want to use that information. You can either use
nl.melt$naam <- reorder(nl.melt$naam, as.numeric(nl.melt$afk))
or
nl.melt$naam <- reorder(nl.melt$naam, as.character(nl.melt$afk), FUN=min)
depending on whether you want to sort by the existing levels of afk or if you want to sort alphabetically by the levels of afk.
After running that and re-running the ggplot code, i get
An alternative to #MrFlick's approach (based on the answer #CarlosCinelli linked to) is:
ggplot(nl.melt, aes(x=interaction(naam,afk), y=perc, fill=stemmen)) +
geom_bar(stat="identity", width=.7) +
scale_x_discrete(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
coord_flip() +
theme_bw()
which gives:
R tends to see the order of levels as a property of the data rather than a property of the graph. Try reordering the data itself before calling the plotting commands. Try running:
nl.melt$naam <- reorder(nl.melt$naam, nl.melt$afk)
Then run your ggplot code. Or use other ways of reordering your factor levels in naam.

Reorder levels of facet_wrap using ggplot

I have an existing dataset with three factors. I would like to plot these three factors using facet_grid() and have them ordered based on how they are ordered in the dataset instead of alphabetical order. Is this possible to do somehow without modifying my data structure?
Here's the data:
https://dl.dropboxusercontent.com/u/22681355/data.csv
data<-read.csv("data.csv", head=T)
ggplot(data, aes(time,a, color="one")) +
geom_line(linetype=1, size=0.3) +
scale_y_continuous(breaks=seq(0,1,0.2)) +
scale_x_continuous(breaks=seq(100,300,50)) +
theme_bw() +
geom_line(aes(time,b)) +
geom_line(aes(time,c)) +
geom_line(aes(time,d))+facet_wrap(~X.1)
This question appears quite too often on SO. You've to get the desired column (by which you're facetting) as a factor with levels in the order you desire, as follows:
data$X.1 <- factor(data$X.1, levels=unique(data$X.1))
Now, plot it and you'll get the facetted plot in the desired order.

Trying to keep filled bars in a faceted plot

Not sure what I'm doing wrong here. I have this plot:
ggplot(data.PE5, aes(ybands,fill=factor(decide))) + geom_bar(position="dodge")
which produces:
Then I want to facet by a factor, creating two stacked plots w/ dodged, colored bars
ggplot(data.PE5, aes(ybands,fill=factor(decide))) + geom_bar(position="dodge") +
facet_grid(~group_label)
However, I lose the factor-based coloring, which I want to keep:
If you move the fill into the geom_bar it should work. As:
ggplot(data.PE5, aes(ybands)) + geom_bar(aes(fill=factor(decide)),position="dodge") + facet_grid(~group_label)
The reason is the way ggplot2 builds plots as a grammar (I think).

Resources