问题描述
我正在尝试用百分比标记堆栈条形图,并以比例结束标记。这是我的代码:
sqlCommand cmd = new sqlCommand("SELECT * FROM[MYTABLE] WHERE([UPN] LIKE '%' + @UPN + '%')",con);
cmd.Parameters.AddWithValue("UPN",UPNCode);
con.open();
DropDownList2.DataSource = cmd.ExecuteReader();
DropDownList2.DataTextField = "State";
DropDownList2.DataValueField = "State";
DropDownList2.DataBind();
if (DropDownList2.Items.FindByText("AL") != null)
{
DropDownList2.Items.FindByText("AL").Text = "Alabama";
}
if (DropDownList2.Items.FindByText("AK") != null)
{
DropDownList2.Items.FindByText("AK").Text = "Alaska";
}
if (DropDownList2.Items.FindByText("AZ") != null)
{
DropDownList2.Items.FindByText("AZ").Text = "Arizona";
}
解决方法
这是一个潜在的解决方案:
# Load libraries
library(tidyverse)
# Create 'fake' data (minimal reproducible example)
stem_data <- data.frame(Grade = rep(c("A","B","C","P","NP"),2),STEMflag = factor(x = c(rep("STEM",5),rep("NONSTEM",5)),levels = c("STEM","NONSTEM")),percent = c(0.95,0.93,0.90,0.67,0.86,0.05,0.07,0.10,0.33,0.14))
head(stem_data)
#> Grade STEMflag percent
#> 1 A STEM 0.95
#> 2 B STEM 0.93
#> 3 C STEM 0.90
#> 4 P STEM 0.67
#> 5 NP STEM 0.86
#> 6 A NONSTEM 0.05
# Plot the example data
ggplot(data = stem_data,aes(x = Grade,y = percent,fill = STEMflag,label = paste(percent * 100,"%",sep = ""))) +
geom_bar(position = "fill",stat = "identity") +
scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
geom_text(position = position_stack(vjust = 0.5),size = 4)
,
jared's answer 可以进一步改进:
-
scales::label_percent(accuracy = 1)
可用于格式化geom_text()
和scale_y_continuous()
中的 种标签一致且无需重复代码立> -
geom_bar(stat = "identity")
可以缩写为geom_col()
# function to format percent labels
lp <- scales::label_percent(accuracy = 1)
library(ggplot2)
ggplot(data = GradeSTEM_data,label = lp(percent))) +
geom_col(position = "fill") +
scale_y_continuous(labels = lp) +
geom_text(position = position_stack(vjust = 0.5))
数据
GradeSTEM_data <- data.frame(
Grade = factor(rep(c("A",levels = c("A","NP")),0.14))