Displaying bars instead of lines with ggplots (R) - r

I'm trying to show this histogram as filled bar, and not lines, that would overlay. I've tried to change "geom_line(alpha=.5)" for geom_bar(alpha=.5), without success.
What's the proper way to display bars in this situation?
require(reshape2)
library(ggplot2)
fileName <- paste("/degree_hist-104-50.csv", sep = "")
mydata = read.csv(fileName,sep=",", header=TRUE, check.names=FALSE)
dataM = melt(mydata,c("F"))
ggplot(data=dataM, aes(x= F, y=value, colour=variable, fill=variable)) +
geom_line(alpha=.5) +
theme_bw() +
theme(plot.background = element_blank(), panel.grid.minor = element_blank(), #panel.grid.major = element_blank(),
axis.line = element_blank(),
legend.key = element_blank(), legend.title = element_blank()) +
scale_y_continuous("Density", expand=c(0,0)) +
scale_x_continuous("Value", expand=c(0,0)) +
theme(legend.position="bottom") +
theme(axis.title.x = element_text(vjust=-0.3, face="bold", size=12)) +
theme(axis.title.y = element_text(vjust=1.5, face="bold", size=12)) +
theme(plot.title = element_text(lineheight=.8, face="bold"))
Here's the data: https://dl.dropboxusercontent.com/u/73950/degree_hist-104-50.csv
Any clue is appreciated! Thanks!

Related

When using identity input, how do you get ggplot to display % symbol next to value label

The below produces a plot with value labels - from a table containing percentages; NOT raw individual level data. So the stat="identity" argument is used. How would you instruct ggplot to add a % symbol to each value label above the bar in such a case?
#Generate data table
Percent<-c(20,80)
row_labels<-c("Male","Female")
df<-data.frame(row_labels,Percent)
#Plot data table
ggplot(df) + aes(row_labels,Percent,fill=row_labels) + geom_bar(stat="identity") +
labs(x = "",title = " ") + theme(legend.title = element_blank()) + coord_flip()+ theme(legend.position="none") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_blank(), axis.ticks.y = element_blank(), axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank()) + geom_text(aes(label=round(Percent,digits=1)), hjust = -0.15, size = 3)
I have modified aes of the geom_text in the following way:
ggplot(df, aes(row_labels,Percent,fill=row_labels) ) +
geom_bar(stat="identity") +
geom_text(aes(y=Percent+5, label = paste(Percent, "%")) ) +
labs(x = "",title = " ") +
theme(
legend.position="none",
legend.title = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_blank(),
axis.ticks.y = element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank()
) +
coord_flip()
Although the order of layers/adjustments does not matter (you have mentioned coord_flip), I have reorganised the code for better fluency.

Using R to create an image don't want the white border or the title to appear only the PNG image

I'm using the code below to try to create a PNG image that is only the image but there is still some text and a white border.
theplot <- data %>% ggplot(mapping = aes(x,y)) +
geom_point(mapping = aes(color=z), alpha = alpha, size = 0.75) +
scale_color_gradient(low="green", high="blue") +
theme_void() + theme(legend.position="none") + theme(axis.title = element_blank())
I've also tried the following.
theplot <- data %>% ggplot(mapping = aes(x,y)) +
geom_point(mapping = aes(color=z), alpha = alpha, size = 0.75) +
scale_color_gradient(low="green", high="blue") +
theme_void() + theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank())
I'm not very familiar with R so I'm not sure if ggplot is what I should be using to create just an image.
Is it ok like this ?
library(ggplot2)
library(ggthemes)
ggplot(mtcars, aes(mpg, wt)) + geom_point() +
theme(plot.margin=unit(c(0,0,0,0), unit="mm"))
+ theme_fivethirtyeight()
ggsave("myplot.png")
One can take a look at the code of theme_fivethirtyeight:
> theme_fivethirtyeight
function (base_size = 12, base_family = "sans")
{
(theme_foundation(base_size = base_size, base_family = base_family) +
theme(line = element_line(colour = "black"), rect = element_rect(fill = ggthemes_data$fivethirtyeight["ltgray"],
linetype = 0, colour = NA), text = element_text(colour = ggthemes_data$fivethirtyeight["dkgray"]),
axis.title = element_blank(), axis.text = element_text(),
axis.ticks = element_blank(), axis.line = element_blank(),
legend.background = element_rect(), legend.position = "bottom",
legend.direction = "horizontal", legend.box = "vertical",
panel.grid = element_line(colour = NULL), panel.grid.major = element_line(colour = ggthemes_data$fivethirtyeight["medgray"]),
panel.grid.minor = element_blank(), plot.title = element_text(hjust = 0,
size = rel(1.5), face = "bold"), plot.margin = unit(c(1,
1, 1, 1), "lines"), strip.background = element_rect()))
}
I must say I don't understand why that doesn't work with your custom theme. I don't see a major difference.

Use of variables in a function for ggplot2 in R (use in scale_x_discrete)

I have created a function to create a plot in ggplot2, one of the requirementes is to sort the bar chart according to the score results for each bar. For this, I have rearranged the data frame but when I try to use the scale_x_discrete with the function variable (Geog), it just doesnt work:
f_OS_RegionF <- function(Geog) {
#Carrier Mean Score###########
mean_c<-mean(co_dataa$Score)
###########################
#Region=deparse(substitute(Region))
data_carr<-co_dataa
ScorexGeog_C <- cast(data_carr, as.formula(paste(Geog,"Carrier",sep="~")), mean, value="Score")
names(ScorexGeog_C)[2] <- "Score"
ScorexGeog_C[2]<-round(ScorexGeog_C[2],digits=1)
ScorexGeog_C<- ScorexGeog_C[order(ScorexGeog_C$Score), ]
write.table(ScorexGeog_C,file="ScorexGeog_C_F_Check.csv",sep=",",row.names=F, col.names=T)
d <- ggplot(ScorexGeog_C, aes_string(x=Geog, y="Score", fill=Geog))+
geom_bar(stat="identity", colour="white")+
coord_flip()+
scale_fill_manual(values = coloress)+
scale_x_discrete(limits=paste("ScorexGeog_C",Geog,sep="$"))+
theme(axis.ticks = element_blank(), axis.title.y = element_blank(),axis.text.y = element_blank(), axis.title.x = element_blank(), axis.text.x = element_blank())+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank())+
theme(legend.position = "none") +
geom_text(data=ScorexGeog_C,aes_string(x=Geog,y="Score",label="Score"),colour="white",vjust=0.5,hjust=2,size=2.5, fontface="bold")+
geom_text(data=ScorexGeog_C,aes_string(x=Geog,y=0.3,label=Geog),colour="white", angle=0, vjust=0.5, hjust=0, fontface="bold",size=2)+
geom_hline(yintercept=mean_c, colour = "gray",linetype="solid", size =1, alpha=.8)
print(d)
}

Legend linetypes not displaying properly ggplot

I have the following plot:
ggplot(proba[108:140,], aes(c,four, color="a1")) +
geom_line(linetype=1, size=0.3) +
scale_x_continuous(breaks=seq(110,140,5)) +
theme_bw() +
theme(axis.line = element_line(colour = "black", size=0.25),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
theme(axis.text.x = element_text(angle = 0, hjust = +0.5, size=6,color="black")) +
theme(axis.text.y = element_text(angle = 0, hjust = -100, size=6, color="black")) +
theme(axis.ticks=element_line(colour="black",size=0.25)) +
xlab("\nTime-steps")+ylab("Proportion correct\n") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8)) +
geom_line(aes(c,three, color="a2"), size=0.2, linetype=2) +
geom_line(aes(c,one, color="a3"),linetype=3, size=0.8) +
geom_line(aes(c,two, color="a4"), linetype=1, size=0.6) +
scale_color_manual(values=c("a1"="red3", "a2"="red3","a3"="blue3","a4"="blue3")) +
theme(legend.title = element_text(colour="black", size=7)) +
theme(legend.position="bottom" ,
legend.direction="horizontal",
legend.title=theme_blank()) +
theme(legend.text=theme_text(size=7),
legend.background=theme_blank(),
legend.key=theme_blank())
The four lines on the plot are displayed in different linetypes, however the legend does not show these different linetypes only the different colours. I am obviously missing a very simple argument in theme() but I cannot figure out what it is. Maybe I need to specify the linetypes again in scale_colour_manual()?
Here's the data: https://dl.dropboxusercontent.com/u/22681355/proba.csv
proba<-read.csv("proba.csv",head=T)
Here, this is the correct way to do this in ggplot2:
proba <- read.csv("~/Downloads/proba.csv")
proba1 <- proba[108:140,]
proba1 <- melt(proba1,id.vars = 1:2)
ggplot(proba1,aes(x = c,y = value,colour = variable,linetype = variable,size = variable)) +
geom_line() +
scale_x_continuous(breaks=seq(110,140,5)) +
scale_colour_manual(values=c("blue3","blue3","red3","red3")) +
scale_linetype_manual(values = c(2,1,3,1)) +
scale_size_manual(values = c(0.2,0.3,0.8,0.6)) +
xlab("\nTime-steps") +
ylab("Proportion correct\n") +
theme_bw() +
theme(axis.text=element_text(size=6),
axis.title=element_text(size=8),
axis.line = element_line(size=0.25),
axis.ticks=element_line(size=0.25),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position="bottom" ,
legend.direction="horizontal",
legend.title=element_blank(),
legend.text=element_text(size=7),
legend.background=element_blank(),
legend.key=element_blank())
Try to keep all your theme adjustments in one call to theme, otherwise things get pretty messy. Also, you had some calls to theme_* that should have been element_*.

ggplot2 theme with no axes or grid

I am trying to make a plot with no information beyond the data. No axes; no grid; no title; just the plot.
But I keep getting extra margins and padding that I can't remove.
library(ggplot2)
library(grid)
theme_bare <- theme(
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
#axis.ticks.length = unit(0, "lines"), # Error
axis.ticks.margin = unit(c(0,0,0,0), "lines"),
legend.position = "none",
panel.background = element_rect(fill = "gray"),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.margin = unit(c(0,0,0,0), "lines"),
plot.background = element_rect(fill = "blue"),
plot.margin = unit(c(0,0,0,0), "lines")
)
ggplot() +
geom_area (data=economics, aes(x = date, y = unemploy), linetype=0) +
theme_bare
Produces this image:
What I want is this:
I can't figure out how to get rid of the blue and make the dark gray flush with the edges.
Could any one offer some advice?
Here is the way to plot only the panel region:
p <- ggplot() + geom_area (data=economics, aes(x = date, y = unemploy), linetype=0) +
scale_x_date(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) +
theme(line = element_blank(),
text = element_blank(),
title = element_blank())
gt <- ggplot_gtable(ggplot_build(p))
ge <- subset(gt$layout, name == "panel")
grid.draw(gt[ge$t:ge$b, ge$l:ge$r])
From ggplot2_2.0.0 you can use theme_void:
ggplot() +
geom_area(data = economics, aes(x = date, y = unemploy), linetype = 0) +
theme_void()
try
last_plot() + theme(axis.ticks.length = unit(0.001, "mm")) + labs(x=NULL, y=NULL)
you may want to file a bug for the 0 tick length.
If you just want to remove the grid in theme_bw(), you can use:
+ theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

Resources